Common Bash Shortcuts and Their History

When you first start using bash, it's easy to feel lost, and the lack of familiarity often leads to low typing efficiency. Many beginners end up deleting a lot of what they've typed just to fix a typo in an earlier word. Learning some basic keyboard shortcuts can really boost your productivity. In this article, I'll walk you through some commonly used bash shortcuts and share a bit of history behind them, which I hope will be helpful for readers.

Note: This article sticks to everyday language, so some technical terms may not be precise. For detailed technical explanations, please refer to the official documentation.

It's worth noting that terms like shell and terminal might be used interchangeably here. If you want to dive deeper into these concepts, check out these answers:

Many bash shortcuts don't need to be explicitly learned if you're already familiar with emacs, since bash's default editing mode is essentially emacs mode. Of course, if you prefer vi, bash also provides a vi mode (to be exact, both vi mode and emacs mode come from GNU Readline). To switch to vi mode, just add the following line to your .bashrc:

set -o vi

Some commonly used emacs shortcuts in bash include:

ctrl-a  # Move cursor to the beginning of the line
ctrl-e  # Move cursor to the end of the line
meta-b  # Move cursor back one word
meta-f  # Move cursor forward one word
ctrl-w  # Cut the last word
ctrl-u  # Cut everything before the cursor
ctrl-k  # Cut everything after the cursor
ctrl-y  # Paste the last thing cut
ctrl-_  # Undo
ctrl-r  # Incremental search backward
ctrl-g  # Quit and abort

And some commonly used vi shortcuts in bash include:

h       # Move cursor left
l       # Move cursor right
b       # Move cursor back one word
w       # Move cursor forward one word
A       # Move cursor to end of line and enter insert mode
0       # (zero) Move cursor to the beginning of the line (doesn't enter insert mode)
i       # Enter insert mode at the current position
a       # Enter insert mode after the current position
dd      # Delete line (saved for pasting)
D       # Delete everything after the cursor (saved for pasting)
p       # Paste deleted text
j       # Move up through command history
k       # Move down through command history
u       # Undo

These examples were adapted from: Emacs and Vi modes in Bash

By the way, if you're not familiar with either vi or emacs, this might be a good time to pick one up—it could really enhance your efficiency when working with text. If you're interested in learning vim, you can check out my article: "Vim Study Notes: Finding the Perfect Distance from Your Keyboard".

In addition to these text-editing shortcuts, there are many other useful keyboard shortcuts, some of which trace their origins back to the days of typewriters. Let's explore a few scenarios to better understand how they work:

How to Stop a Running Program

While working in bash, it's common to enter the wrong command or find yourself stuck in a loop. When I first started using bash, I often found myself clueless in these situations, especially when I couldn't figure out which key to press to stop or continue a command. Back then, I'd sometimes resort to closing the terminal or even forcefully shutting down the computer. Fortunately, there are simple shortcuts to handle such situations:

Of these, <Ctrl-C> is the more frequently used, but <Ctrl-D> can also be handy, like when you're in the middle of changing a password using the passwd command and decide to cancel it.

For more details, you can check out these two articles:

Why Isn't My Keyboard Responding?

One issue that's even more frustrating than not being able to stop a program is when your terminal suddenly stops accepting input for no apparent reason, even when pressing the shortcuts mentioned above. I once thought my computer or keyboard was broken because of this.

But don't worry—this might just be due to accidentally pressing <Ctrl-S>. If that happens, simply press <Ctrl-Q> to "unlock" the terminal. After you press <Ctrl-S>, the terminal continues to record everything you type, and it will all be processed once you hit <Ctrl-Q>. This feature might seem unnecessary today and even a bit of a nuisance, but it has its roots in older technology. Here's a relevant excerpt from this answer:

Long before there were computers, there were teleprinters (a.k.a. teletypewriters, a.k.a. teletypes). Think of them as roughly the same technology as a telegraph, but with some type of keyboard and some type of printer attached to them.

Because teletypes already existed when computers were first being built, and because computers at the time were room-sized, teletypes became a convenient user interface to the first computers – type in a command, hit the send button, wait for a while, and the output of the command is printed to a sheet of paper in front of you.

Software flow control originated around this era – if the printer couldn't print as fast as the teletype was receiving data, for instance, the teletype could send an XOFF flow control command (Ctrl+S) to the remote side saying "Stop transmitting for now", and then could send the XON flow control command (Ctrl+Q) to the remote side saying "I've caught up, please continue".

And this usage survives in Unix because modern terminal emulators are emulating physical terminals (like the vt100) which themselves were (in some ways) emulating teletypes.

How to Clear a Cluttered Screen

You can use <Ctrl-L> to clear the terminal screen (though it really just hides the current content—you can still scroll up to see it, so don't think you can use this to cover up any secrets! (¬◡¬)✧). This shortcut also has its roots in typewriter usage. Early typewriters required manual resetting of the printing carriage when switching sheets of paper, and this shortcut mimics that process.

For more details, see:

I hope these tips will enhance your experience using bash! :)