Linux optimize

Using vi key bindings in bash and zsh

If you prefer to use vi or vim for command-line editing, you can configure shells to use vi key bindings instead of emacs-style key bindings. Here's how.

By default, most shells use emacs-style key bindings for command-line editing and modification. For users of vi or vim, however, you can instead configure shells to use vi key bindings instead. This is done by editing ~/.bashrc in the case of bash, or ~/.zshrc in zsh and adding:

set -o vi

in bash, and the following for zsh:

bindkey -v

Once you have saved either ~/.bashrc or ~/.zshrc, exit the shell and open a new terminal.

By default, you will be in insert mode, which means that you type as you normally would. Commands like [Ctrl]A or [Ctrl]E no longer work, however. To get into command mode, press [ESC] (as you would in vim) and use vi key commands to navigate the command line, such as $ to go the end of the line or 0 to go to the beginning of the line.

From that point, you can use other vi commands:

  • b to go back one word
  • 2b to go back two words
  • dw to delete a word
  • dd to delete the entire line
  • d$ to delete from the current cursor position to the end of the line
  • d0 to delete from the current cursor position to the beginning of the line
  • w to go forward one word, and so forth

To return to insert mode, simply type i, as you would in vi, and you can edit the command line as you normally would.

If you are more familiar with vim than emacs, using vi key bindings in your shell makes a lot of sense and will make working in the shell much easier as you will be using edit commands that you are already familiar with.

If you use emacs more often, leave the defaults as they are to use the various commands you would normally use in emacs. On the other hand, if you use neither, choose a style that makes the most sense and makes using the command line faster and more efficient.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

5 comments
Photogenic Memory
Photogenic Memory

This is a great article. This is what I like about open source. You can modify so many things that it gives you a lot of control over the programs you use and the output you can make them generate. Cool stuff! I hate EMACS. Everytime I 've tried to warm up to it; it smacks me in the face. Maybe I'm lazy. But vi is fun to use for basic users like me. Anyways; thanks for posting this. This looks like a fun project to implement for kicks! Thanks.

DGermantr@Real-World-Systems.com
DGermantr@Real-World-Systems.com

after the initial [ESC] [keypad up arrow] recalls the previous command [keypad up arrow] previous-previous ... A append to the end. [ESC] to terminate input I insert in beginning. [ESC] to terminate input [keypad right] moves, well, to the right! i insert before cursor.[ESC]... a insert after cursor.[ESC]... /cd searches for & recalls the previous cd command n previous-previous cd n previous-previous-previous cd N previous-previous cd

wratholix
wratholix

I would say emacs is more for writing publications etc than it is for editing system files.. i never use it.

TJ111
TJ111

There's a lot of other neat little tricks (outside of whether your in vi or emacs mode) that I find surprising more people don't know about. '!!' - Repeat the last command '!x' - Where x is the first letter of a command. This repeats the last command that starts with the letter 'x'. Very nice when your doing alot of file operations in different directories and stuff 'cd' - By itself it's basically a "go back", where it goes back to the last directory you were in. There's plenty more, but just a few I thought of when I read your post.