Visual Guide for Becoming Better at the Command Line

Visual Guide to Quickly Learning and Remembering Readline Commands

Marcellus Pelcher
4 min readDec 4, 2016

--

You are at a Bash terminal and you type something like:

grep --i text some/long/path/file.txt another/long/path/file.txt

You get an error saying that ‘ --i’ is ambiguous! You forgot that one letter arguments should have one ‘-’. What do you do? It is easy to fall into the trap of pressing the up arrow and then holding down the left arrow to get to the place where you can remove the extra ‘-’. If you are like me, a part of you dies inside because you know there is a better way. You probably have also seen the commands that allow you to go faster, but don’t recall them. If this sounds like you, I wrote this blog for you!

Learning Techniques

I will not only show you the commands but also show techniques for easier recall. I will use chunking to reduce learning 9 commands to 6 commands. I chose 9 of the most useful commands out of the 49 commands from the Readline Cheat Sheet, equating to 18% of the list. Learning 18% is in line with the 80/20 rule. Learning 20 percent of the commands will get you 80 percent of the benefits. Commands with a counterpart that goes in the opposite direction are illustrated with a QWERTY keyboard. Notice the commands that operate on characters to the left are placed on the left and the commands that operate on characters to the right are placed on the right. If you are familiar with Emacs, these keybindings will be familiar.

Searching

Ctrl-R Searches previous commands.
You could press the up arrow until you see your command, but there is a faster way.

After you press Ctrl-R, an interactive prompt appears. While you type, it will match previous commands. The example shows ‘world’ being typed. To iterate through matches, Ctrl-R was used again. After I had learned this, I felt bad for all the time I wasted but felt good for all the time I will now save.

Moving

Ctrl - A Moves to the beginning of the line.
Ctrl - E
Moves to the end of the line.

Ctrl-E can be remembered by thinking ‘End.’ Ctrl-A can be remembered because it is the first letter of the alphabet.

Meta - F Moves forward one word.
Meta - B
Moves back one word.

Instead of Ctrl, you use the Meta key. The Ctrl version of these commands only moves one character at a time. In some terminals, you have to change the terminal’s setting to use Alt key as the Meta key. These two keys can be remembered as F for ‘forward’ and B for ‘back.’

Editing

Ctrl - U Removes everything to the left of the cursor.
Ctrl - K
Removes everything to the right of the cursor.

A way to remember these two commands is to say K ‘kills’ everything to the right. Because they are positioned right next to each other, if you can recall one, you can usually recall the other.

Ctrl - W Deletes from the cursor to previous whitespace.
Meta - D
Deletes from the cursor to the end of the current word.

This combo is a little confusing, and I am not sure why the designers made the default mapping this way. I think it is because they behave slightly differently. Ctrl - W uses white space for the deleting boundary, whereas Meta - D removes characters until it sees a non-alphanumeric character.

If you found this helpful, please click on the 💚 button.

Bonus Commands

Ctrl- X Ctrl - U Undo last edit.
Tab Auto-completes Bash command.
Ctrl - L Clears screen.
Ctrl - Y Allows you to resurrect what you ‘killed’ in another location.

Additional Resources

Readline man page
Readline Cheat Sheet
Thoughtbot - Readline
Configure OS X terminal to use the Alt key as the Meta key
Configure iTerm to use the Alt key as the Meta key

--

--