Command-line keyboard shortcuts for Bash
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.
This one is so fundamental that it could be omitted, but:
- There was a time that I didn’t know it.
- If you aren’t aware that it exists, you are unlikely to discover it on your own.
- It is a tremendous time-saver.
- 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:
- To avoid scrolling through hundreds of commands, pipe the output of history through grep.
- To execute the desired command, type !<command-number>.
- 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.
- To avoid moving your hand to the arrow keys, replace up-arrow and down-arrow with Ctrl-P and Ctrl-N.
- 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:
- Ctrl-A: Goto start
- Ctrl-E: Goto end
- Ctrl-K: Delete to end
- Alt-B: Back word
- Alt-F: Forward word
- Alt-D: Delete word
- Ctrl-B: Back character
- Ctrl-F: Forward character
- 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.