#bash #linux #terminal
One of my favorite things about the terminal is that there is always something
new to learn. Many years ago I accidentally typed <ctrl> + a and
cursor jumped to the beginning of the line. I ended up searching for more
keyboard shortcuts in the terminal and started adding them to my repertoire.
These are just the essential shortcuts that I use daily. There are many more if you're interested in looking them up!
Note: some of these shortcuts use the
<meta>key. This is usually the<alt>key on linux/windows and the<option>key on macos. Though you likely have to configure your<alt>key to<option>on macos.
Using previous commands
When you want to reuse a recent command, you could push ↑ or ↓.
Or instead you could learn the shortcuts...
<ctrl> + p: previous command in history<ctrl> + n: next command in history
This is really handy to memorize as you don't have to move your hands over to the arrow keys for some history navigation.
Search history
Know of a command you've used somewhat recently? You can quickly pull it into
the current command instead of searching for it with history | grep
for example.
<ctrl> + r: reverse-incremental search through command history- subsequent
<ctrl> + r: pull up the next matching command in the search
Note: you can safely press enter once you find the command you were searching for. It will bring that command into your current command and not execute. Allowing you to modify it before execution if you wish.
Jumping around the current command
Being able to move your cursor around the current command quickly is really handy when you memorize the following shortcuts.
<ctrl> + a: jumps to the beginning<ctrl> + e: jumps to the end<meta> + f: jumps forward a word<meta> + b: jumps backward a word
Fun fact:
<ctrl> + (a|e)comes from the Emacs text editor and is natively supported in macos in most of their GUI programs.
Clearing the scrollback buffer
Everyone knows that running clear will wipe the scrollback buffer in your
active session. But a lesser known keyboard shortcut is running...
<ctrl> + l: clears the screen but preserves scrollback buffer
Note: this is similar to
clearbut doesn't technically wipe the scrollback buffer. Instead it just shifts it off the screen.
Exiting a shell
Everyone also knows that running exit will logout and exit the current shell.
But instead you can also do...
<ctrl> + d: exit current shell
Cutting from your current command
Quickly removing items from your current command is handy when you want to run a command a second time, but need to change a quick piece of it.
<ctrl> + w: cut the previous word<meta> + d: cut the next word<ctrl> + k: cut text to end of line<ctrl> + u: cut text to beginning of line
Note: these commands not only delete parts of your input, but also store them in a temporary buffer. We will cover this in the next section!
Pasting from your temporary buffer
If you cut text with any of the above shortcuts, they will be stored in a temporary buffer for you to use. This is handy if you want to quickly cut some text from your previous command, and use it in your next command.
<ctrl> + y: paste from temporary buffer
Reusing the last parameter in a command
Bash has some handy (and questionable) history expansion commands. This one in particular is really useful if you run a command against a file and then immediately want to run another against the same file.
!$: replace with the last argument of the previous command
Note: history expansion is typically disabled when running in a non-interactive shell. If you want to do something like this in a script for example, you can instead use
$_. Though I think that use case would be really rare in a script.