bash readline emacs editing mode default keyboard shortcut cheat sheet

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.


  • 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-<number> shortcut is bound to readline's numeric-argument function which in some cases acts as how many times should the following command be repeated.

Here is a real word example, suppose we have typed the following ([] is the cursor):


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':


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!

Download Emacs Editing Mode Cheat Sheet

PDF format (.pdf):
Download link: readline emacs cheat sheet (.pdf)
Downloaded: 75144 times

ASCII .txt format:
Download link: readline emacs cheat sheet (.txt)
Downloaded: 17647 times

LaTeX format (.tex):
Download link: readline emacs cheat sheet (.tex)
Downloaded: 9908 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!


October 31, 2007, 04:37

the cheat sheet of screen utility was very useful for me. Waiting eagerly for VI's cheat sheet now.

October 31, 2007, 04:41

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.

Sarandi Permalink
May 17, 2014, 05:10

Would you be able to outline when and why you use one mode versus the other? I know both have their strengths/weaknesses as programs so I am assuming there are some key bindings that are more efficient/intuitive in one mode versus the other (?).

Soliko Permalink
October 31, 2007, 09:56

Nice one,
Do you know how how to use AltGr (right alt) in PuTTY as regular Alt?

Roman Permalink
October 31, 2007, 21:56

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.

bleh Permalink
November 01, 2007, 14:47

You could have copy-pasted the page from the bash manual instead of the readline man 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 acknowledging
the copyrights of these manual pages....

bleh Permalink
November 01, 2007, 14:49

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 02, 2007, 01:50

ok..that would be even better. However, I use ksh instead of bash. Any plans for ksh users?

November 04, 2007, 06:27

Ankush, I do not have any plans at the moment for ksh cheat sheet...

November 05, 2007, 01:30

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 20, 2007, 20:27

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 19, 2008, 12:49

Thanks for some nice tips ;)

I use vim as my editor of choice although I prefer emacs mode for bash.

January 19, 2008, 12:57

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 20, 2008, 04:58

Martins, thank you for correcting me.

June 07, 2008, 20:50

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.

Jeremy Permalink
June 14, 2008, 14:30

I've been Linux/Bash for nearly 10 years now and I didn't know half of these :/

cjk Permalink
June 14, 2008, 23:41

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

firbush Permalink
August 12, 2008, 22:11

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.


lelutin Permalink
October 30, 2008, 15:12

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 ;)

March 18, 2010, 12:20

You should probably define what M- means before you use it, for those who are not familiar with Emacs conventions. Also, does't M- usually mean Alt? Why is it Esc now?

May 17, 2010, 03:48

Hi, please, have a look at my cheat sheet:

Nikos Permalink
September 27, 2011, 03:47

Does ANYONE know how i can go to the next occurrence of a character within a command? Let say that I am the beginning of the following command:

if [ -f `which notify-send` ]; then echo "yes"; fi;

How do I get to the second t with one (or two) keystroke?? I do not mean to count the spaces and apply M+6 M+F!! I mean the equivalent of "2ft" of vi mode!

Thank you!

Icarus Permalink
September 29, 2012, 06:48

Expecting to do it in one keystroke is unrealistic! One could special case it, so a function key did search for the second t, but how often would you use that feature?

On my system, with bash 4.2, I would type

M-2 C-] t

Meta 2         sets the argument to 2
Control ]      is bound to 'character search'
t              is the character to search for.
Yves Permalink
November 25, 2012, 14:40

The cheat sheet tells that "emacs-editing-mode" short cut is "C-e" while it is actually the shortcut for "end-of-line".

I tried to guess what could be a good shortcut to activate "emacs-mode" in bash. So I was curious and typed in "C-x C-e" in a bash window. And, awesome, it opens an emacs instance with the current line in a buffer.. Of course I guess it is the correct shortcut for "emacs-editiing-mode", am I right? :-)

Lee Doolan Permalink
May 28, 2014, 04:32

I have to tell you about one of my faves:

$ !<histnum>M-^

bang-history-meta-hat brings the command out of the history ring into the command buffer and leaves the cursor at the end of the line.

try it, you'll like it.

mazhar Permalink
September 09, 2014, 06:42

thanks for the useful stuff, i have saved it

ramu Permalink
January 02, 2015, 19:15


Leave a new comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "cdrom_35": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.