Friday, October 13, 2006

GDB: Readline vi Mode

Only the ardent most of vi lovers will appreciate this...
I was debugging my program that was SIGSEGV'ing. After a couple of runs, my left-right and up-down arrow keys stopped working in gdb! Annoyed I switched the termnial to have a second look at the code, when I returned to GDB terminal... lo and behold... in my instinctive way used vi's 'k' and 'j' keys! BINGO! I just found out that there indeed is a vi mode in gdb too! And I bet not many know that, and hence this post.

If you don't care reading any further, you can enable vim editing mode by pressing: Ctrl-Alt-j Now pressing Esc will enter you in command mode.

For the more persistent readers...
Being an ardent lover of vim, the only TRUE programmer's editor (Emacs is a nice OS, but it lacks a good editor, that's why I use vim), I do all my work in vi: be it coding, writing mail, compose documents, blog, etc. etc. Anything that you can think of which requires writing more than few sentences, I do it in vim. In any other editor, I feel handicaped! So much so, that I even use vi mode on my BASH shell! That way I can really type and manipulate anything with eye-flashing speed. (Tip: This can really impress people [...cough ...cough...] at times ;-) )

$ set -o vi
This will put your command editing in vi mode. Press ESC and you get all of vi cursor movement and most of text editing commands. So when I found this under gdb, I was just flabbergasted! It's called 'vim readline mode' in gdb. Damn! Why I didn't think of this earlier, the world's MOST versatile debugger ought to have such features!

Now I won't have to move my hands off in brain-dead fashion at snail slow speed to and from the arrow keys! One of the best things about vim is if, like me, you are a touch typist and prefer typing really fast (for you don't want to miss anything that's on your mind), you won't want to move your hands off their position on keyboard often. And this is _exactly_ where Emacs fails, miserably! A tech-tip post after a long long time.

1 VIM Tip-1
2 VIM Tip-2
3 Gdb Readline vi Mode


jetxee said...

This is really sweet to have vi mode in bash :)

Anonymous said...

Ah, a fellow traveller! Thanks, I was looking (hard) for that M-C-j (emacs notation) for switching gdb's command edit mode.

AaronG said...

Is there any way to permanently enable vi readline mode? Or even another way to enable this mode without using meta+j (it's captured by my window manager)

The Shaolin said...

AaronG, putting a line "set editing-mode vi" in your ~/.gdbinit file should have it enabled by default every time you run gdb, without you having to go through meta-key sequence.
Let me know if this helps.