Essential Shell Shortcuts

Here's a list of some really awesome shell (bash/zsh/etc) shortcuts that I use all the time.
, Updated: 2 min read

This post contains some shortcuts that I’ve found to be incredibly useful. (Some of these are directly from the manpage of readline(3))

Most of these work for bash, and other shells that use the readline style shortcuts such as zsh. The same goes for history expansion commands like !!.

#1 - Last command shortcut

The string !! expands to the entirety of the last command run. Forgot to use sudo on a command? no problem, just type sudo !!

#2 - Last argument of the last command

The variable $_ is set to the last argument of the last command run. Very useful if doing lots of stuff with the same file.

#3 - Search the history

!gcc will expand to the last command that starts with gcc. If you’re not sure what you;re looking for, CTRL+r is your friend. type to search your history.

#4 - Beginning/End of the line

CTRL+a/CTRL+e - While editing a command, you can jump to the beginning of the line by pressing CTRL+a, and to the end by pressing CTRL+e.

#5 - Clear the screen

CTRL+l - Much quicker than typing clear

#6 - Quick replace

Replace “emacs” from the last command with “vi”, type ^emacs^vi^

#7 - Quick Shell Exit

CTRL+d - just like typing “exit”, but much faster.

#8 - Pipe into “while read line”

This lets you execute a series of commands for each line of text that’s piped in, via read and a for loop:

cat file.txt | while read line; do echo "$line"; done

Also, we can change the delimeter that read uses with the -d flag.

#9 - Customize your shell / editor

You can customize your shell using the config files in your home directory. For example, in bash, you can add aliases to your .bashrc file, or in zsh, you can add them to your .zshrc file. Vim also has a .vimrc file for the customization as well.

There are tons of examples of customized configs (and some pre-built ones you can use, like Oh My Zsh). I publish mine on this site, and the latest versions are here: https://rich.sh/2024/04/07/dotfiles-refresh

Subscribe to my Newsletter

Like this post? Subscribe to get notified for future posts like this.

Change Log

  • 10/12/2017 - Initial Revision
  • 10/12/2017 - Renamed article to be titled "Essential Bash Shortcuts" from "My favorite bash shortcuts"
  • 10/12/2017 - Added #8: Pipe into "while read line"
  • 4/16/2024 - Renamed to "Essential Shell Shortcuts", since most of these work in other shells too.

Found a typo or technical problem? file an issue!