I am now on Twitter! Meet me on Twitter here (my nick is pkrumins.)
Or on Google Buzz and Facebook.
When you are working in a shell you certainly don’t want to waste your time using arrow keys or home/end keys to navigate around the command line. One of the most popular shells, bash - Bourne Again SHell, uses GNU’s Readline library for reading the command line.
The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in. The readline library also includes functions to maintain a list of previously-entered command lines, to recall and perhaps reedit those lines, and perform csh-like history expansion on previous commands. Both emacs and vi editing modes are available.
I have mastered both of the editing modes and have created cheat sheets for both of them (and a tiny separate one for readline’s history expansion).
This is a cheat sheet for the default, emacs, editing mode.
Here are a few examples with screenshots on how to use this editing mode.
Let ‘[]‘ be the position of cursor in all the examples.
Example 1: movement basics
Suppose you are at the end of the line and want to move 3 words backwards.
$ echo word1 word2 word3 word4 word5 word6[]
If you hit M-3 followed by M-b, you would end up exactly where you wanted:
$ echo word1 word2 word3 []word4 word5 word6
An alternative is to hit M-b three times in a row: M-b M-b M-b
If you look up on the cheat sheet what M-3 does, it sets the numeric-argument to 3 which in this case acts as a counter how many times should M-b command be repeated. The M-b command calls backward-word function which does the obvious.
The numeric-argument can also be negative, which makes the argument to be applied in the opposite direction.
Other shortcuts of interest are M-f to move forward and C-a, and C-e to move to the beginning and end of line.
Example 2: command history
Suppose you used a pretty complex command a while ago and now you remember just a few arguments of this command. You want to find this command and call it with a few arguments modified.
If you hit C-r readline will put you in an incremental reverse history search mode. Typing a part of the arguments you remember, will locate the previously executed command matching the typed text. Hitting C-r again will locate any other command which matches your typed text.
To put the found command on command line for editing hit C-j.
Example 3: completing
Suppose you want to quickly list all the users on the system.
Hit C-x ~ and read-line will attempt username completion and output all the usernames to the terminal.
$ [] adm catonmat ftp halt mailnull nobody root smmsp vcsa apache cpanel games lp mysql nscd rpc sshd bin daemon gopher mail named operator rpm sync cat dbus haldaemon mailman news picurls shutdown uucp $ []
Suppose you now want to quickly list all the users on the system starting with ‘m’. You can type ‘m’ followed by the same C-x ~ to do that.
$ m[] mail mailman mailnull mysql $ m[]
The other interesting completions are:
- C-x / which lists possible filename completion,
- C-x $ which lists possible bash variable completion,
- C-x @ which lists possible hostname completion and,
- C-x ! which lists possible command completion.
and
- Meta-/ which does filename completion,
- Meta-$ which does bash variable completion,
- Meta-@ which does hostname completion and,
- Meta-! which does command completion.
Example 3: killing and yanking
Suppose you have to type a-long-word-like-this a couple of times.
The easiest way to do this is to kill the word, which puts it into the kill ring. Contents of the kill ring can be accessed by yanking.
For example, type ‘a-long-word-like-this’ in the shell:
$ command a-long-word-like-this []
Now press C-w to kill one word backward:
$ command []
Press C-y to yank (paste) the word as many times as you wish (I pressed it 3 times here:)
$ command a-long-word-like-this a-long-word-like-this a-long-word-like-this []
The kill ring does not contain just the one latest killing. It can be filled with a number of kills and rotated with M-y shortcut.
Another example:
Suppose you typed a longer command and you noticed that part of the THE TEXT GOT TYPED IN CAPITAL LETTERS. Without knowing the readline shortcuts you would erase the text and probably type it again. Now you can use the readline keyboard shortcuts and change the case very, very quickly.
You can use the following shortcuts to accomplish this:
1) M-l (Meta-l (on your computer, probably ESC-l)) shortcut is bound to readline’s downcase-word function which lowercases the current word.
2) M-b shortcut is bound to readline’s backward-word function which moves the cursor one word backwards.
3) M-
Here is a real word example, suppose we have typed the following ([] is the cursor):
$ echo the text. THE TEXT GOT TYPED IN CAPITAL LETTERS[]
To get to the beginning of ‘THE’ we might repetitively hit M-b seven times or we could set the numeric argument to seven by typing M-7 and then hit M-b once.
After doing this the cursor would have moved before the word ‘THE’:
$ echo the text. []THE TEXT GOT TYPED IN CAPITAL LETTERS
Now, by setting the numerical argument to 7 again and by pressing M-l or by pressing M-l seven times, we turn the text all in lower case.
$ echo the text. the text got typed in capital letters[]
Actually what we did in this example was not as efficient as it could have been. The numeric-argument shortcut accepts negative arguments which turn the direction of the following command in other direction. We could have turned the text in lower case by hitting M--7 and M-l
If you really want to be more productive, I suggest you play around with the commands in the cheat sheet for a while.
My previous article on being more productive on the command line was screen’s cheat sheet which allows to emulate multiple terminals in a single window. You can take a look at it as well!
PDF format (.pdf):
Download link: readline emacs cheat sheet (.pdf)
Downloaded: 16883 times
ASCII .txt format:
Download link: readline emacs cheat sheet (.txt)
Downloaded: 4298 times
LaTeX format (.tex):
Download link: readline emacs cheat sheet (.tex)
Downloaded: 1932 times
This cheat sheet is released under GNU Free Document License.
The next cheat sheet will be readline’s vi editing mode’s default keyboard shortcut cheat sheet! :)
Did you like this post? Subscribe here:
If you really enjoyed the post, I'd appreciate a gift from my geeky Amazon book wishlist. Books would make me more educated and I could write even better posts. Thanks! :)


(13 votes, average: 3.85 out of 5)
|
|
|


October 31st, 2007 at 4:37 am
the cheat sheet of screen utility was very useful for me. Waiting eagerly for VI’s cheat sheet now.
October 31st, 2007 at 4:41 am
Ankush, it will not be the vi (editor) cheat sheet! It will be a cheat sheet for vi keyboard bindings when working with bash!
As I said in the article, bash uses readline library to get input from you. When you type something (for example a command ‘ls -las’) it’s the readline library that gets this text for you.
The library has two editing modes - emacs mode and vi mode. Each of these modes has its own keyboard shortcuts for doing stuff like moving a word back, clearing the whole line, etc.
This article provided a cheat sheet for emacs mode.
The next one is going to be a cheat sheet for vi mode.
I personally use both modes and switch between them as needed.
October 31st, 2007 at 9:56 am
Nice one,
Do you know how how to use AltGr (right alt) in PuTTY as regular Alt?
October 31st, 2007 at 9:56 pm
Uhh, ‘Rubout’? I guess you should have clarified on this, because I realized it was the backspace only after several-seconds-long intense staring at the backwards-delete-char line. 8=]
Curiously, the Wikipedia page about Rubout redirects to Delete. Go figure.
November 1st, 2007 at 2:47 pm
You could have copy-pasted the page from the bash manual instead of the readline man page……you wouldn’t have missed the other completion keys…
Also unbound functions on a cheat sheet what’s the point?
I guess it’s too much work…like acknowledging
the copyrights of these manual pages….
November 1st, 2007 at 2:49 pm
these are not even in your cheat sheet…
* C-x / which does filename completion,
* C-x $ which does bash variable completion,
* C-x @ which does hostname completion and,
* C-x ! which does command completion.
truly another masterpiece….
November 2nd, 2007 at 1:50 am
ok..that would be even better. However, I use ksh instead of bash. Any plans for ksh users?
November 4th, 2007 at 6:27 am
Ankush, I do not have any plans at the moment for ksh cheat sheet…
November 5th, 2007 at 1:30 am
Bleh, the unbound keys are there so people knew they existed and could re-map them. Also, if you look carefully enough you will notice it is released under GNU FDL, the same license as readline man page.
I did not include those keys you listed in the cheat sheet because I ran out of space there. So I left them out…
November 20th, 2007 at 8:27 pm
Great stuff! I can’t count how much more quickly I could have finished my PhD if I’d only known (or rather, be bothered to look up) shortcuts like this.
January 8th, 2008 at 11:18 pm
[…] 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. […]
January 19th, 2008 at 12:49 pm
Thanks for some nice tips ;)
I use vim as my editor of choice although I prefer emacs mode for bash.
January 19th, 2008 at 12:57 pm
Actually after I tried out a few tips that I liked I see that you have described them a little bit wrong.
This is wrong:
* C-x / which does filename completion, * C-x $ which does bash variable completion, * C-x @ which does hostname completion and, * C-x ! which does command completion.They just show possible completions. The actual completions are done with Meta key + / or $ or @ or ! respectively.
January 20th, 2008 at 4:58 am
Martins, thank you for correcting me.
February 18th, 2008 at 11:34 pm
[…] efficiently in bourne again shell. Previously I have written on how to work efficiently in vi and emacs command editing modes by using predefined keyboard shortcuts (both articles come with cheat sheets […]
June 7th, 2008 at 8:50 pm
I’d put an explanation of the meta-key next to your first example of its use. For emacs neophyte seing M- is confusing :)
I was happy to see it later:
1) M-l (Meta-l (on your computer, probably ESC-l)) shortcut is bound to readline’s downcase-word function which lowercases the current word.
June 14th, 2008 at 2:30 pm
I’ve been Linux/Bash for nearly 10 years now and I didn’t know half of these :/
June 14th, 2008 at 11:41 pm
Modern key replacements for emacs keys:
M-b: Alt-Left
M-f: Alt-Right
C-a: Pos1
C-e: End
C-j: Enter/Return
And other goodies:
C-k, kill until EOL.
C-l, clear screen
C-d (it’s a tty setting though), logout
August 12th, 2008 at 10:11 pm
Thanks, very useful. My co-workers use emacs on the command line, so I’d like to transition for their sakes. This is very helpful.
Thanks,
October 30th, 2008 at 3:12 pm
Hey, I found another one in the documentation that is quite useful:
M-. cycles through previous commands’ last argument.
This page pushed me to try and learn bash shortcuts and as a result, I got faster and more efficient. Thanks :)
oh and cjk, those are good to know but the ones in the cheat sheet concentrate on letters and keys that are centered on the main chunk of the keyboard. By using the letters and stuff near them, you get to move your hands around the keyboard less often, which makes you operate faster ;)
August 14th, 2009 at 7:45 pm
[…] Bash Emacs Editing Mode Cheat Sheet: http://www.catonmat.net/blog/bash-emacs-editing-mode-cheat-sheet/ […]