r/programming • • Jan 08 '08

Working Productively in Bash's VI Command Line Editing Mode

http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/
45 Upvotes

18 comments sorted by

9

u/pkrumins Jan 08 '08 edited Jan 08 '08

The cheat sheet contains almost all (tough, I might have missed some) shortcuts of bash's vi editing mode.

I examined the source code of bash carefully to find all the default key bindings.

Here are download links for the cheat sheet:

Have fun :)

3

u/ramen Jan 08 '08

For zsh users, you can switch editing modes with bindkey and the following options:

          -e     Selects keymap `emacs', and also links it to `main'.

          -v     Selects keymap `viins', and also links it to `main'.

          -a     Selects keymap `vicmd'.

Type "man zshzle" for more information.

3

u/pkrumins Jan 08 '08 edited Jan 08 '08

Yeah, and bash users, you can switch editing modes with following commands:

      set -o emacs    Switch to Emacs Mode

      set -o vi       Switch to VI Mode

I also made an Emacs mode shortcut cheat sheet, but it's not as good as this one :(

ps. how did you get that constant font comment?

2

u/ramen Jan 08 '08

I just pasted that from the man page. I'm not really sure what did it. Maybe starting with indentation and a hyphen?

2

u/pkrumins Jan 08 '08

I just added more spaces, and it turned to constant font wonderfullness :)

Thanks!

4

u/boredzo Jan 08 '08

Even easier, put this in your .zshenv file:

export EDITOR=vi

Yes, that's all you have to do. As long as this is in one of your startup files, zsh will see it and do the Right Thing.

5

u/mxyzptlk Jan 08 '08

Some of the most fun I've had at work involved changing the boss's editing mode from vi to emacs.

1

u/bennymack Jan 09 '08

As usual I feel obliged to mention that certain other command line programs (such as MySQL) can be switched to VI mode with the following key strokes if the proper libraries are installed: CTRL+[ CTRL+j

I find it funny that projects like Octave have a section documenting how to use the CLI which basically describes emacs mode when vi mode works equally as well (or better.)

1

u/pkrumins Jan 11 '08

That's the default readline key binding for switching from emacs mode to vi mode (man readline).

Bash (in bashline.c) undefines this key combination with a comment that it is unnecessary because one can change the modes via set -o vi and it might be confusing to users if they hit it accidentally!

0

u/pkrumins Jan 11 '08

That's the default readline key binding for switching from emacs mode to vi mode (man readline).

Bash (in bashline.c) undefines this key combination with a comment that it is unnecessary because one can change the modes via set -o vi and it might be confusing to users if they hit it accidentally!

2

u/cratuki Jan 08 '08 edited Jan 08 '08

I thought I was the only one who used it :) I wish the '.' command worked as it does in vi. I frequently desire that and have never had any need for the current usage.

It's good to use this in conjunction with gnu/screen, where ctrl+a behaviour is altered by screen itself. If you want to change the escape key, I think you can put 'escape ``' in a file at $HOME/.screenrc.

2

u/[deleted] Jan 09 '08

I feel so stupid for not knowing a vi mode existed.

It comes with the added bonus of being able to turn gnome-terminal shortcuts back on.

1

u/bajsejohannes Jan 09 '08

I've entered vi commands in bash countless times before. Finally they actually work!

Some issues though:

  • I can't tell the difference between insert and command mode, which is pretty annoying.

  • Some keys, like home and end, stopped working. They render ~ instead. I don't see this in either bash emacs mode or in vim.

Anyone know how to get around these? I'm using putty as a terminal.

1

u/[deleted] Jan 09 '08 edited Jan 09 '08

I can't tell the difference between insert and command mode, which is pretty annoying.

True. But if you discover you are in the wrong mode, you can type "u" in command mode to undo changes.

Some keys, like home and end, stopped working. They render ~ instead. I don't see this in either bash emacs mode or in vim

Never use "home" and "end". Use ^ and $ instead.

<esc> is painful to reach, too. Use <ctrl>+[

1

u/bajsejohannes Jan 09 '08

Never use "home" and "end". Use ^ and $ instead.

Sure, I could. But I want to use home and end for both speed (it's one keystroke, you know) and out of habit. Who says I can't? (ps. it doesn't matter if it is not working on some obscure terminal)

<esc> is painful to reach, too. Use <ctrl>+[

That sure makes it better, but not as easy (for me) to do as home and end.

2

u/pkrumins Jan 09 '08

If working in INPUT mode use neither home/end, nor switch to COMMAND mode and use ^ and $.

Put these two lines in .inputrc

 "\C-a": beginning-of-line
 "\C-e": end-of-line

Which will bind CTRL-a to go to beginning of line, and CTRL-e to end of line.

Be sure to bind CTRL-a of screen to other key.

1

u/ator_fighting_eagle Jan 11 '08

<esc> is painful to reach, too. Use <ctrl>+[

I've been trying to do this recently (with my capslock remapped to ctrl) and my results are mixed. The difference betweeen '{' and ctrl-[ is small enough that I mix them up all the time. My full-sized keyboard has a shrunken 'esc' key, but if it didn't I'm not sure esc would be too hard. It's a little twitch to hit it, rather than a controlled keystroke combo.

Anyone have any other solutions?