By default, most shells use emacs-style key bindings for command-line editing and modification. For users of vi or vim, however, you can 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 to 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.

Advertisement

Open Sourcery This was published in Open Sourcery, check every Monday for more stories

Related links

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Staff Facebook's portal for the masses

    This week, Facebook took a number of strategic steps toward its goal of giving people the "power to share and make the world more open and connected." That's how founder and CEO Mark Zuckerberg described the mission statement for Facebook. Read more »

    -- posted by Staff

  • Brendon Chase Do you trust data in the cloud?

    Cheap hosted storage, app engines, and hosted code libraries. Can you really trust your data, or your client's data in the magical Web 2.0 cloud? Read more »

    -- posted by Brendon Chase

  • Staff The future remains yesterday

    Remember when MySQL was blazingly fast and cared little for SQL standards? When MySQL regarded a view as something nice from your window and a trigger was treated as a weaponry component? Those days are set to return with a MySQL fork called Drizzle. Read more »

    -- posted by Staff

What's on?