Code and comments

Practical and theoretical aspects of software development

Command-line keyboard shortcuts for Bash

with 3 comments

I love keyboard shortcuts, and I do my best to incorporate them with every GUI application that I use regularly. I’ve been comparatively negligent in learning keyboard shortcut for the command-line applications, as it seems less advantages. After all, there’s no need to reach for the mouse, what are we hoping to save?

Nevertheless, there are real efficiencies out there, and here are my favorite Bash shortcuts.

Tab completion

This one is so fundamental that it could be omitted, but:

  1. There was a time that I didn’t know it.
  2. If you aren’t aware that it exists, you are unlikely to discover it on your own.
  3. It is a tremendous time-saver.
  4. It exists in many (most?) CLI aplications, not just Bash.

Tab completion means, “Press Tab so that the computer finishes your thoughts when the meaning is obvious.” This is essential when navigating deep directory structures. If you have typed enough characters to specify the desired directory, then Tab finishes the name. If not, then Tab gives you choices:

$ cd s[press Tab]
scripts/        src/           stacktrace.log

$ cd s

The tab completion behavior of the windows command-line is different, as it chooses the first possibility, and allows you to cycle through the choices by pressing Tab repeatedly.


If you find yourself using long commands repeatedly, you want to re-use them, to save time in typing, to avoid having to think of all of the details of the commands, and to avoid wasting time with typing errors.

The most common way to access the command-history is by using the up-arrow and down-arrow to view old commands, then editing or pressing enter. But this isn’t as useful if you know that many commands have occurred since the desired command. In that case, the history command is useful.

To get the most out of history:

  1. To avoid scrolling through hundreds of commands, pipe the output of history through grep.
  2. To execute the desired command, type !<command-number>.
  3. To edit the desired command, type !<command-number>:p, which will print the command and place it in the command history. Then up-arrow will place the command in the command-line for editing.
  4. To avoid moving your hand to the arrow keys, replace up-arrow and down-arrow with Ctrl-P and Ctrl-N.
  5. Edit: Don’t fail to read about Ctrl-R in Nathan’s comment below — which may make you question the value of grepping history.

Navigation and Editing

When commands get long, navigating and editing them can get painful. If you have ever typed a command that wrapped onto a second line, you don’t want to use the arrow keys as your only option for putting a forgotten ./ at the start. Fortunately, many of the Emacs keyboard shortcuts are available in Bash. Here are the navigation and editing commands that I have found the most useful:

  1. Ctrl-A: Goto start
  2. Ctrl-E: Goto end
  3. Ctrl-K: Delete to end
  4. Alt-B: Back word
  5. Alt-F: Forward word
  6. Alt-D: Delete word
  7. Ctrl-B: Back character
  8. Ctrl-F: Forward character
  9. Ctrl-D: Delete character

Some of these are more obviously useful than others. You may be particularly skeptical of the usefulness of the last three, as they can be replaced with left-arrow, right-arrow, and Del. It all depends on how you feel about moving your hands to a different part of the keyboard. Since I use a variety of keyboards regularly, I consider it a significant win to never move my hands away from the home keys. But if this seems excessive, then just make sure to use Ctrl-A, Ctrl-E, and Ctrl-K when appropriate, as they are the biggest time-savers in this group.

How about you?

These are my favorites, but I’m sure that I’m missing many useful shortcuts. What do you find most helpful? Comment below.


Written by Eric Wilson

October 25, 2011 at 5:39 am

Posted in how-to

Tagged with , , ,

3 Responses

Subscribe to comments with RSS.

  1. For a comparison of keyboard shortcuts among various command-line environments, check-out:

    Eric Wilson

    October 25, 2011 at 9:43 am

  2. To choose a command from your history based on what’s contained in it, use Ctrl+R, then start typing the part of the command that you remember. It’s google for your history. If you find the right command, enter invokes it, or Esc copies it to the command line for editing.

    Also, if you like history and completion, but have some CLI application that doesn’t support it, try the command-line utility rlwrap. For example, let’s say you had some interactive command line app like an interpreted language or database administration tool that gives you a prompt and expects commands, and you try using up/down arrows but just get funny characters instead of history support. Instead of running the tool with its name (say, exampletool) you’d run it as “rlwrap exampletool” instead. rlwrap == readline wrapper, and it wraps the tool in an input interceptor that lets you do history, tab completion, etc. from within the app’s own prompts.


    October 25, 2011 at 10:05 am

  3. On bash command prompt, type “set -o vi”. Hit escape now it becomes vi mode. Type any vi shortcuts like “i” to scroll upward or type “/search[Enter]”. To go to home type “^” to go to beginning of the line and similarly “$” to the end of the line. To edit a command “Esc-i” or “cw” to change word etc etc. I love the vi-mode at command line. Similarly, you can change to Emacs mode if you like that.


    November 5, 2011 at 11:20 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: