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.

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

$ 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!

Download Emacs Editing Mode Cheat Sheet

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

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

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

Comments

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 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....

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.

Thanks,

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: http://jan.tomka.name/project/readline-commands-cheatsheet

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? :-)

Harier Permalink
April 08, 2014, 19:51

We have dissertation proposal at cheap prices. We write the quality essay services for students.

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.

June 05, 2014, 14:40

Thanks for sharing the worth to read article with us

June 19, 2014, 15:56

Thanks for this great article.It was worth reading it...

June 19, 2014, 15:56

Thanks for this great article.It was worth reading it...

July 03, 2014, 11:23

Ok it will be amazing

July 23, 2014, 11:59

Make the stuff and look

August 01, 2014, 12:39

Thanks for the Cheat.
News. It worked for me

August 04, 2014, 05:25

Is so great chance

August 16, 2014, 17:28

Nice Bash Emacs Editing Mode Cheat Sheet superb things..

Thanks for sharing..

August 16, 2014, 18:55

THANKS FOR AN WONDERFUL ARTICLE

August 17, 2014, 13:20

oka laila mp3 songs download

nagachaitanya's oka laila kosam telugu mp3 songs download

August 22, 2014, 14:07

Nice man!
This is really helpful for me

August 28, 2014, 21:49

Hello thanks for share a great guide and You have done a good job. I will definitely share and personally recommend to my friends,relatives.

August 28, 2014, 21:50

very nice article

August 28, 2014, 21:50

kaimzzzzzzzzzzz

August 28, 2014, 21:51

awsum article...thanx

August 28, 2014, 21:52

wow..............

August 28, 2014, 21:52

Nice Bash Emacs Editing Mode Cheat Sheet superb things..

Thanks for sharing..

September 07, 2014, 19:11

Teachers day full quotes

hello you must check this stuff :) thank you

mazhar Permalink
September 09, 2014, 06:42

thanks for the useful stuff, i have saved it

September 12, 2014, 08:10

thanks for sharing such a nice trick. If you guys need to know about India and want whats happening there, India Trendings & Happenings just follow this blog.

September 12, 2014, 08:13

if u r always on mobile, want to download bollywood music videos, then Hindi Movies Video Songs FREE Download is for you, just visit and enjoy.

September 20, 2014, 20:28

As we know already, iPhone 6 and iPhone 6 Plus are now available in stores of US and is estimated to come to India on 17th of October officially.
But here is a good news for all Indian iPhone lovers, yes we can pre-order iPhone 6 & iPhone 6 Plus now in India, prices unveiled too. An e-commerce website Grabmore has grabbed the opportunity and is taking pre-orders in India with a free shipping on the iPhone 6 and iPhone 6 Plus.
The website promises to ship the products iPhone 6, iPhone 6 plus between 1st of November and 13th of November 2014.
This is not an official sale, these iPhones will be purchased from the American market and shipped to India.
But this is generating much more excitement on me. Yess.. I love iPhone.
New Hindi movie mp4 video songs download. Thanks

khaja Permalink
September 22, 2014, 17:26

Know A Job for all latest Jobs.

khaja Permalink
September 22, 2014, 17:27

Latest Job News for all latest Jobs.

Nanda kumar Permalink
September 25, 2014, 06:50

Good post on bash.I like it. Are you looking forward to easiest, fastest and low-cost methods for your company to achieve enterprise mobility management? Click here to know more about it.

October 02, 2014, 22:59

Thanks for it.

Nothing Permalink
October 03, 2014, 06:54
October 07, 2014, 10:49

Amazing Post

October 10, 2014, 14:27

goo post Columbus Day 2014 thanks

October 19, 2014, 05:52

Thanks For the Stuff...I Have bookmarked your site to check your new post. This stuff is so much interesting. Feeling great after reading this.

October 21, 2014, 17:54

Wow! Something (kinda) great. Obviously!

October 21, 2014, 17:56

Blogging Tips
you should be aware of!

October 21, 2014, 17:56

Wow! Got a great blog!

November 12, 2014, 15:22

Merci pour l'astuce.
Bonne continuation.
Cordialement.

November 13, 2014, 14:15

Thanks for sharing.

November 13, 2014, 14:16

cool bro Thanks!

November 13, 2014, 14:24

Results

November 14, 2014, 10:35

Thanks

November 15, 2014, 11:53

Superb Post.

November 15, 2014, 17:42

This is very insightful

Thankyou

November 17, 2014, 15:18

Nice information. thanks for sharing.

November 17, 2014, 15:18

thanks for sharing.

November 18, 2014, 07:28

Importantly, Ctrl-Y allows you to paste the text that you just deleted with any of the above key combination.

November 18, 2014, 12:34

awesome post
sexy

Thanks

November 18, 2014, 12:34

awesome post
Free

Thanks

November 18, 2014, 12:37

awesome post
images
Videos
World
Download
Picture
HD

Thanks

November 19, 2014, 10:35

Aweosme post ... Thanks

November 21, 2014, 09:29

Nice Post, Thanks for a sharing

Dubai Shopping Festival at UAE 2015
New Years Eve Party Dubai

Keep Sharing.

Alisha Permalink
November 21, 2014, 16:15

Thankyou so much for the information Peteris.I just went through your post, you have crafted it well. See, if these will work or not.
Merry Christmas 2014 HD Wallpaper
Actually I am trying to create a new program which helps the user to access the content which are not worth banning, but banned so in the country.digg
Man Your site comes to great help for me.

November 21, 2014, 16:54

Wow! At last I got a weblog from where I be able to actually get useful information regarding my study and knowledge.
CAT 2014 Results.

November 22, 2014, 11:10

Thanks for sharing the worth to read article with us

November 22, 2014, 15:02

Get Valentines Quotes Valentines Ideas For Him 2015

November 26, 2014, 10:28

Thanks a lot for sharing such a great content with us. Your website is just amazing with lots of helpful articles and it helped me a lot. Thanks for sharing this with us.
KVS Admit Card 2014
MIDC Admit Card 2014
UPPSC Revenue Inspector Admit Card 2014 - 2015
Thanks once again.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

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

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

Advertisements