Follow me on Twitter for my latest adventures!
Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)
Bash provides two modes for command line editing - emacs and vi. Emacs editing mode is the default and I already wrote an article and created a cheat sheet for this mode.
This time I am going to introduce you to bash's vi editing mode and give out a detailed cheat sheet with the default keyboard mappings for this mode.
The difference between the two modes is what command each key combination (or key) gets bound to. You may inspect your current keyboard mappings with bash's built in bind command:
$ bind -P abort can be found on "\C-g", "\C-x\C-g", "\M-\C-g". accept-line can be found on "\C-j", "\C-m". alias-expand-line is not bound to any keys ...
To get into the vi editing mode type
$ set -o vi
in your bash shell (to switch back to emacs editing mode, type set -o emacs).
If you are used to a vi text editor you will feel yourself at home.
The editing happens in two modes - command mode and insert mode. In insert mode everything you type gets output to the terminal, but in the command mode the keys are used for various commands.
Here are a few examples with screenshots to illustrate the vi editing mode.
Let '[i]' be the position of cursor in insert mode in all the examples and '[c]' be the position of cursor in command mode.
Examples:
Once you have changed the readline editing mode to vi (by typing set -o vi), you will be working in insert mode.
The example will be performed on this command:
$ echo arg1 arg2 arg3 arg4[i]
Example 1:
Suppose you have typed a command with a few arguments and want to insert another argument before an argument which is three words backward.
$ echo arg1 (want to insert arg5 here) arg2 arg3 arg4[i]
Hit 'ESC' to switch to command mode and press '3' followed by 'B':
$ echo arg1 [c]arg2 arg3 arg4
Alternatively you could have hit 'B' three times: 'BBB'.
Now, enter insert mode by hitting 'i' and type 'arg5 '
$ echo arg1 arg5 [i]arg2 arg3 arg4
Example 2:
Suppose you wanted to change arg2 to arg5:
$ echo arg1 [c]arg2 arg3 arg4
To do this, you can type 'cw' which means 'change word' and just type out 'arg5':
$ echo arg1 arg5[c] arg3 arg4
Or even quicker, you can type 'f2r5', where 'f2' moves the cursor right to next occurrence of character '2' and 'r5' replaces the character under the cursor with character '5'.
Example 3:
Suppose you typed a longer command and you noticed that you had made several mistakes, and wanted to do the correction in the vi editor itself. You can type 'v' to edit the command in the editor and not on the command line!
Example 4:
Suppose you typed a long command and remembered that you had to execute another one before it. No need to erase the current command! You can switch to command mode by hitting ESC and then type '#' which will send the current command as a comment in the command history. After you type the command you had forgotten, you may go two commands back in history by typing 'kk' (or '2k'), erase the '#' character which was appended as a comment and execute the command, this makes the whole command look like 'ESC 2k0x ENTER'.
These are really basic examples, and it doesn't get much more complex than this. You should check out the cheat sheet for other tips and examples, and try them out!
To create the cheat sheet, I downloaded bash-2.05b source code and scanned through lib/readline/vi_keymap.c source code file and lib/readline/vi_mode.c to find all the default key bindings.
It turned out that the commands documented in vi_keymap.c were all documented in man 3 readline and I didn't find anything new.
After that I checked bashline.c source file function initialize_readline to find how the default keyboard shortcuts were changed. I found that 'CTRL-e' (which switched from vi mode to emacs) got undefined, 'v' got defined which opens the existing command in the editor, and '@' which replaces a macro key (char) with the corresponding string.
The cheat sheet includes:
- Commands for entering input mode,
- Basic movement commands,
- Character finding commands,
- Character finding commands,
- Deletion commands,
- Undo, redo and copy/paste commands,
- Commands for history manipulation,
- Completion commands,
- A few misc. commands, and
- Tips and examples
Download Vi Editing Mode Cheat Sheet
PDF format (.pdf):
Download link: bash vi editing mode cheat sheet (.pdf)
Downloaded: 165803 times
ASCII .txt format:
Download link: bash vi editing mode cheat sheet (.txt)
Downloaded: 35210 times
LaTeX format (.tex):
Download link: bash vi editing mode cheat sheet (latex .tex)
Downloaded: 10017 times
This cheat sheet is released under GNU Free Document License.


Facebook
Plurk
more
GitHub
LinkedIn
FriendFeed
Google Plus
Amazon wish list
Comments
Thanks! It's finally here after a long wait :)
It works equally well on ksh too.
Ankush, yeah, it's finally here :) Great to hear that it works well on ksh, as well!
Next cheat sheets are going to be on Linux IPTables packet flow (which I created some 4 years ago), and I'll have another one on bash's history expansion.
I had no idea this was possible! Thanks a lot!
Same loyal readers, great articles everytime.
No, u slona vse ravno ...
Thanks, will test tonight
Hello Peter,
Thanks for visiting and commenting on our blog! We are plesantly surprised that you came upon our blog.
Your video lectures are quite impressive! And we, at the EIC (www.thehnoeic.wordpress.com)in Maryland, hope you get into MIT :)
Thanks,
Shaj Mathew
Is there a way to tell visually if you are in command mode or insert mode from the terminal in a visual way? Like in vim you can setup the INSERT to display when you are in insert mode. Thanks.
Brian, as far as I know it is not possible to visually tell whether you are in insert mode or command mode.
In the 1st example you don't need to use capital B, it's enough with just b.
Martins, yup, I don't need a capital B in this particular example but if argN, where N = 1, 2, ..., was a more complex one like "(foo)", then the the lowercase 'b' would treat ')' and '(' as separate words...
nice cheat sheet Peter :),
i was wondering if it was possible to get visual feedback in what mode you currently are.
do you have any idea's on that?
grtz
Mischa, as far as I know that is not possible. But it's not that confusing. I am mostly working in vi mode, and now have bound the most useful of emacs bindings to vi mode, so I got a mixed vi/emacs mode.
Really Helpful :-)
Thanks for the guide :-)
"... have bound the most useful of emacs bindings to vi mode, so I got a mixed vi/emacs mode."
Can you give some tips on how to do this? Did you edit the readline source? I'd really love to have ^P in insert mode go up just as in normal mode.
thanks for the cheats got them in time for a exam and was very helpful in remembering those syntax .... i bow to the great ones thanks a bunch keep 'em coming see ya
Peter,
Just what I needed. Thanks!
This is killer !!!
I haven't read anywhere about this bash's feature
I toyed around with bash edit modes years ago and for some reason although I prefer vim wholeheartedly over emacs I have gotten to prefer the emacs edit mode at the commandline. I think it's because, gnu readline is a widely used library, so therefore I can do ctrl-a to get to the front of the line in bash and in firefox text boxes and in etc etc.
The vi edit mode for bash would be nice, but like many people say it's not really great for general computing. Since I log into over 10 different boxes for different things and get csh, bash or whatever depending, I'd rather have something that just works. On the systems I use a lot, I will customize my environment a bit, especially if my home is exported across different machines, but in general I go plain vanilla.
As long as I have a long merging history set, I'll use whatever shell pops up by default.
:-)
Just my $0.08 or so... judging by the length.
Actually, where I said 'vi edit mode for bash would be nice' I meant, it would be nice with the following improvements.
1) easy way to switch between vi and emacs mode with a single keystroke
2) a way to see what mode I was in when in vi mode
I know there I times I would really like to be bouncing around the command line with /'s and n and hjkl and all those goodies, but I meant to say I just never have the time to switch to it and generally don't need it.
bind a key to emacs-editing-mode
Sweetness!
JT
Ultimate Anonymity
There is a link to the Emacs mode in the article, too. Both are very cool!
Instead of using set -o vi you add 'set editing-mode vi' to you $HOME/.inputrc file you have vi-keybinding-loveless in all applications that support readline. You can also set this to system wide by added this to /etc/inputrc.
Ummmmmmmm.
Hey -
Very interesting stuff. Is there any way to get this to support text objects like in vim? For example ciw for "change inner word." This would also be great to edit command args in quotes, i.e. ci" or ci'.
In recent bash versions it's possible with some inputrc sauce: https://github.com/minos-org/minos-bash-settings/blob/master/etc.inputrc
pets head deliver frog tom university
look green australia me head deliver night we juicy we woman all night girl german
australia english jhon bag bag red joke steven green
Looked at vi mode cheat sheet, text version. Is there an error under Basic Movement? Then, "h" s/b left and "l" s/b right. Thanks for well laid out easy to read cheat sheet.
cheers
it works perfectly in cygwin !
Thank you for your work on the cheat sheets. I'll definitely be adding your blog to the read list.
Thank you very much!
thx dude, great article
becauese M-k == ESC k, you can just use M-k to switch into command mode; also M-h M-l M-j M-w ...
hi Peter, thanks very much for pointing out that āvā is defined to open the command itself in an editor (which will be $VISUAL, not $FCEDIT, by the looks of bashline.c).
Since 'v' is used a lot in vim for visual highlighting, it is easy by habit to accidently hit the v-key at the bash commandline while editing in the vi-mode. Plus calling in $VISUAL is probably overkill in most situations...
It seems extremely difficult to remap v for other purposes, unlike the other keys. Maybe it's impossible, but I would appreciate hearing from you first ;-) Thanks very much.
PS- (set keymap vi-insert) is probably the closest to vim among four other vi-choices.
vi mode where it pops in like an (arg: 3) just sucks.
Very distracting and fucked up. Those asshole kind of solutions looking for problems need to be options, not the default action.
Some jerk doesn't have enough to do so they fuck up perfectly good software.
You should ban the person above, and delete their comment. How rude!
Although I do agree that having to learn UNIX just so I can edit a file E.G. 'edit file.ext' is a bit anoying! :)
Hi! I was surfing and found your blog post... nice! I love your blog. :) Cheers! Sandra. R.
Chris, actually he's right. Wholeheartedly, I really like Bash with its Vi-mode but this (arg: 1) thing is plain confusing. Is it possible to disable it somehow?
Once again, Peter, you are shedding lots of light in some dark corners of the Unix world. I have subscribed to your feed and look forward to not only updates, but reading your older stuff, which is new to me.
one thing that is awkward about vi is having to reach for the esc to switch modes key, which nowadays is usually located in a corner of the keyboard
there's another way to switch. ctrl-[
this might be on the other side of your keyboard so it gives you a way to switch modes from the opposite side
also, hitting v to jump into your editor seems like a "annoyance" until you begin creating user-defined functions in your interactive shell. or maybe you like saving short scripts.
e.g. you are typing up a good one-liner and then you decide to make it a function and save it for later use. you add give it a name() and brackets { ;} and then hit v. :w name :q and you're done. this is the essence of quick and dirty.
you can load these functions on the fly in other one-liners too, using sed. like this:
commands | sed '1i\
. nameoffunction
' | sh
i don't use bash. i use a very basic bourne-like shell that's stripped of "features" but close to being POSIX compliant. but with vi mode, tabcomplete and functions it's more than enough. vi mode is a good thing. and there are alternatives to readline if you look around.
i just noticed the ctrl-[ shortcut is mentioned in the post about command line history. well done.
one more thought: someone above mentioned the ubiquity of readline and emacs bindings. and the desirability of one standard set of bindings that works "everywhere". but if we are focusing on unix and the command line (cf, e.g., gui apps), i wonder if /bin/sh is not even more ubiquitous than readline. every system that runs scripts is likely have sh.
so from any shell one could type /bin/sh and then set -o vi. and then one is in familiar territory.
and according to the open standards group, if it's a terminal that supports command line editing, it should have vi mode.
i like both types of bindings, but i wonder which is really more ubiquitous.
new to the bourne shell...any goo tutorials? anything will b appreciated
on ksh (perhaps on bash, who knows), you might consider also adding one of
set -o vi-esccomplete
or
set -o vi-tabcomplete
to change the file and command completion behaviour.
This was an excellent post. I saw some things I didn't know about. Btw, you can also search your history with '/' while in edit mode. So, if you know you've ssh'd into a machine but don't remember how long ago you did it, you can do a '/ssh' and find it (use 'n' for next find 'N' for previous).
Nice post, Peter.
Just a couple of remarks:
In your Examples 1 and 2, for completeness and to be precise, you could mention that the user should hit the Enter key at the end, to actually execute the modified commands.
And likewise, in Example 3, that they should hit and then Enter, to exit the temporary file brought up by vi for the v command, and that the command gets executed immediately (IIRC) when they type that key sequence - you don't get a chance to edit it more, as I recall.
Other than those two minor nits, useful article, and it's cool that you dug through the source to find info for it ...
- Vasudev
Aargh! due to use of angle brackets to indicate colon, etc., my above comment got garbled. Here is what I really meant:
For this:
"they should hit and then Enter"
substitute this:
"they should hit colon w q and then Enter"
- Vasudev
Didn't notice the message about using "<" to insert a "less than" sign, before posting.
Ok, you !%$# blog software: I meant "and lt semicolon" - finally ! :-) I hope ...
Two things that keep me using tcsh are:
* automatically switches to command mode on history or arrow key
* uses tab-completion by default
That said I would like to use bash's loop-rollup and function aliasing,
but since I use command mode more than anything the ROI isn't there.
FINALLY!!
I'm an old HPUX user and KSH and default vi mode command line editing are seemingly hard wired into my digits. BASH was really getting tiresome. This find will save my sanity (or what's left of it). Thanks for writing this!
Now if I could only remap the escape key to the spot God intended it to be...where the caps lock is. Anyone who used to use old HP workstations knows exactly what I'm talking about.
Tweezak,
Not sure if you'll see this, but if you'll create a file (I call mine .xmodmap-esc) with these lines:
clear Lock
keycode 0x42 = Escape
And then load it with:
xmodmap $FILENAME
Nice article, great!I like it, thanks for sharing it with us. its worth reading!
this is incredible.. i can't believe i didn't know about this before. thanks!
Fantastic! Now I just wonder if anyone has written a script to automatically match your bash vi mode key maps to your existing Vim key maps. That would save me a whole lot of grief and appease the DRY gods.
Thanks to your guide, the vi mode has become even more useful!
However, I found one downside which is due to a "feature" introduced in bash 4.2: http://lists.gnu.org/archive/html/bug-bash/2011-02/msg00274.html
This broke my path completion completely and I didn't find a way to get the 4.1 behaviour back. Also the workaround suggested in above mentioned thread seems to be available only in emacs mode (afaics vi mode doesn't support the 'shell-expand-line' command.
Any hint is greatly appreciated!
Firstly thanks for the wonderful cli-experience-changing tutorial!
I have but one gripe that I'm trying to sort out. Whenever I try to edit the command in 'vi' itself by pressing the 'v' key, it opens up in nano!
If someone would be willing to show how to fix this, it'd be much appreciated.
You need to update your $EDITOR variable to reflect the editor that you prefer.
Add this line to your .bashrc:
export EDITOR=vim
Oh my god, how the hell did I not know about this? This seriously makes my life so much better, thank you so much! I was sick and tired of having to hold down the arrow keys to get to the beginning of the line!
You can use CTRL-A and CTRL-E in emacs mode (default) to jump to the beginning and end of a line. Works in any textbox in OSX, actually.
thanks... this works for me. I though the vi functionality in command line won't work in bash. :-)
Thanks for explaining the vi at the bash line. Didn't know about the "v" editing - nice. Also, at the bash line, Alt-l can be substituted for Esc. It is much easier. Further Alt-l can be used in most vims. Where it can't, I like :imap
<M><Esc>The editor got me. The previous comment was to be Alt - lower case L pressed at the same time. The imap should read :imap < M - l > < E s c > without the spaces
Also in input mode, Alt-s works as a convenient backspace. Too bad it doesn't work in gvim etc.
You just changed my life
Thanks a lot for this :)) I really love vi and always wondered how to use it to edit bash command line... then I was studying `bind` builtin and discovered this wonderful ability to use readline commands. Then googling gave off your blog, and it is excellent!
Didn't even know that in default mode I was using emacs commands. OK I'll see if the "mixed" emacs+vi mode could help.
Thanks again!!!
Thank you.
Hello Peter
Really nice cheatsheet.
I also came here to tell you that you can abtain the same vi mode by editing ~/.inputrc and adding
set editing-mode viThis will enable the vi mode in bash, zsh, ksh and every other application that uses libreadline.
Leave a new comment