Skip to content

Bash hotkeys and tricks

These are my most-used hotkeys and tricks with the command line.

Bash has a lot of functionality (the man pages are literally 6500+ lines), with hotkeys, aliases and shortcuts, but much of it is not really more efficient than using the mouse or typing stuff manually, unless you are in an environment where you for example don't have the mouse, or the terminal is very slow.

This is a collection of the hotkeys I've actually internalised and helps me be (or at least feel) more efficient in a "normal" environment.

They may seem like tiny gains (and maybe they are), but it adds up when you constantly save a few seconds by avoiding:

  • typing long words or symbols
  • backtracking because of typos
  • tiny frustrations

Completion

Hotkeys that makes the shell do more of the typing for you.

Tab

Goes without saying for many people, but it is my most used key by far. I probably use this between every 5 other keystrokes on average, just for good measure. It never hurts, sometimes does nothing, mostly helps. Single-tap it to autocomplete unambigiously, double-tap it to list alternatives. It:

  • completes file paths
  • helpfully enters the slash character for you in path names
  • completes environment variable names
  • completes commands and command arguments (if this doesn't work, make sure you have the bash-completion package installed).
  • hints at availably command arguments (note that it might not be 100% accurate or complete, it depends on the content of the bash-completion script for the specific command)

Tip

It is possible to create custom completion files for your custom scripts. That is out of scope for this article, but you'll find good resources on it elsewhere.

Alt+.

Probably my second most used combination. Inserts the last argument from the previous command.

This might sound obscure and a bit silly, but is surprisingly good, because of how arguments on the command line is usually structured (e.g. filename goes last).

For example:

mkdir -p my_directory/with_the/longest-name_possible # make a nested directory
cd [alt+.] # navigate to the newly created directory
# or
ls -l myfile # list a file
cp [alt+.] [alt+.].bak # make a backup of the file
# or
vim myscript.sh # edit a file
./[alt+.] # execute the script you just edited
git add [alt+.] # stage it with version control

You can also tap it multiple times to get the last argument from the next most recent command, etc.

For example, for the following history:

~ $ history
(...)
1234 vim test.sh
1235 ls -l
1236 echo test1 test2
  • First Alt+. inserts test2
  • Second Alt+. inserts -l
  • Third Alt+. inserts test.sh
  • etc.

Tip

You can also use Alt+0 then Alt+.

A variation of the Alt+. hotkey. Will paste the 0th argument of the previous command (which is actually the base command itself!)

It also works with other numbers than 0, but keeping track of which parameter has which index gets difficult and is error-prone, so I find it is mostly its better to copy-paste or write it manually at that point. 0 (first) and . (last) is easy to keep track of.

Useful when you have a command that you need to run more than once, but it is difficult to write or shares the first letters with other commands, making Tab completion less effective:

  • firewall-cmd --add-port=123/udp then firewall-cmd --reload. Last command becomes: ([alt+0][alt+.] --reload)
  • systemctl status myservice then systemctl restart myservice. Last command becomes: ([alt+0][alt+.] restart [alt+.])

Also useful when you want to use the man page for the last command, like man [alt+0][alt+.]

Ctrl+D

Basically runs the exit command when your prompt is empty. If you've started writing something in the terminal, this hotkey does nothing.

It will let you:

  • Exit out of your sudo -i shell
  • log out your current serial or SSH session
  • quit your interactive Python interpreter
  • exit out of your mysql client shell
  • end your tmux session
  • and probably much more

without writing the exit commands explicitly.

Ctrl+R

Reverse command search.

  • You know what your command is, but you don't feel like typing it all.
  • You remember that you need to export a variable, but you don't remember the exact details.
  • You just want to quickly re-run the last vim command without scrolling through the history with Up.

Just use Ctrl+R, then search for something unique to your command.

Didn't find it on the first try? Just run Ctrl+R again for the next match.

Mistyped it? Use Ctrl+C and try again.

Want to edit it? Use Ctrl+A or Ctrl+E when you've found it (more info on these below).

!!

This basically just means "the entire last command". I've found about two uses for it, but I use them pretty often:

sudo !! - Repeat the last command, but with sudo

watch !! - Run the last command over and over automatically to watch for changes.

Tip

There are a lot of these "Event designators", starting with !, but this is the one I actually use. You can also use !123 for the 123rd entry in your bash history, !-1 for the latest minus 1 command, etc. The issue is that you won't know for sure what the command represents until you either run it, or expand it with Esc Ctrl+E.

Editing

Commands to make changes to the current work-in-progress command and terminal output.

Ctrl+L

Clears the screen, giving you a fresh start without needing to hold Enter forever.

Why not just use clear?

  • If you're in the middle of writing something in the prompt, that will be preserved.
  • Also Ctrl+L is faster.

Ctrl+A and Ctrl+E

To move the cursor to the start (Ctrl+A) and end (Ctrl+E) of the current line.

Ctrl+U

Cuts/removes anything to the left of the cursor.

Less used, but the opposite is Ctrl+K which cuts/removes anything to the right of the cursor.

Ctrl+Y

Will paste whatever you've last cut with Ctrl+U.

A very useful combination is Ctrl+E Ctrl+U, which removes the entire line in your prompt. Now you can run another command, maybe an ls command to ensure you are doing the right thing, before pasting the other command back with Ctrl+Y and running it.

Ctrl+W

Cuts/removes one word at the time, to the left. Note that if you run multiple Ctrl+W commands without doing something else in between, the words will be added to the paste buffer, so that when you run Ctrl+Y later, all the words you deleted with Ctrl+W sequentially will be pasted back.

I use it mainly to remove chunks of the prompt quickly though.

Ctrl+S and Ctrl+Q

You probably used Ctrl+S before by accident, "freezing" your terminal. It can be unfrozen with Ctrl+Q.

But it can be used intentionally too, for example if you want to highlight/copy something before some other output scrolls it away, or if you just want an extra second to read whatever the output says.

Depending on your terminal emulator, multiplexer, line history, etc. you might be able to scroll back up and get it later, but why wait when you can just pause and do it right now?

Alt Left and Right

To jump word-by-word instead of letter-by-letter through your current command.

Ctrl+X Ctrl+E

Opens an editor (determined by the EDITOR and VISUAL environment variable), including whatever you've already wrote on the prompt. Very useful for commands with many parameters like complex curl commands.

Save and quit to execute it, quit without saving to cancel it.

Tip

If you get an error like bash: emacs: command not found..., that means you haven't set the EDITOR or VISUAL variable, defaulting it to emacs. Set it with export EDITOR=vim or whatever you prefer, and add it to your ~/.bashrc to make it permanent.

Directory navigation

cd

Navigates to your home folder, the same way cd ~, cd $HOME and cd /home/$USER/ does.

cd -

Will navigate to the previous folder you were in. Very nice when you need to jump to another folder temporarily, then go back.

cd ../my/other/folder
ls -l
cd - # to go back to where you were

cd ~foo

Go the the home folder of the foo user. Mostly useful when logged in as root.

This is just a nice little shortcut when your home folder is in /home but very useful if you don't know where the home folder is.