DEV Community

Cover image for 10 Terminal Shortcuts That Feel Like Cheating (bash & zsh)
Muiduzzaman Lipu for Refactorly

Posted on

10 Terminal Shortcuts That Feel Like Cheating (bash & zsh)

If you still press the up arrow fifty times to find an old command, this one's for you. These ten shortcuts work in both bash and zsh, on macOS and Linux, and none of them need a plugin.

Video version:

1. Ctrl+R: search your history

Press Ctrl+R and type any part of an old command:

(reverse-i-search)`dock': docker compose up -d --build
Enter fullscreen mode Exit fullscreen mode

The shell shows the most recent match as you type. Press Enter to run it, press Ctrl+R again to jump to an older match, or press → / Esc to edit it first. Of all ten, this is the one that will change how you use the terminal.

2. sudo !!: rerun the last command with sudo

!! ("bang bang") expands to the previous command:

$ cat /etc/sudoers
cat: /etc/sudoers: Permission denied
$ sudo !!
sudo cat /etc/sudoers
Enter fullscreen mode Exit fullscreen mode

The shell prints the expanded command before running it, so you always see what actually ran. !! works anywhere, not just after sudo:

$ echo hello world
hello world
$ echo again: !!
echo again: echo hello world
again: echo hello world
Enter fullscreen mode Exit fullscreen mode

3. !$: reuse the last argument

!$ is the last argument of the previous command. It's perfect after creating something with a long path:

$ mkdir -p my-new-project/src
$ cd !$
cd my-new-project/src
Enter fullscreen mode Exit fullscreen mode

In zsh (and bash with readline), Alt+. inserts the last argument right at the cursor, so you can see it before pressing Enter.

4. cd -: jump back

cd - takes you to the previous directory, like a browser's back button, and prints where you landed:

$ cd /var/log
$ cd ~/projects/app
$ cd -
/var/log
Enter fullscreen mode Exit fullscreen mode

Run it again to toggle back. It's great for bouncing between two folders.

5–8. Edit long commands without the arrow keys

Shortcut What it does
Ctrl+A / Ctrl+E Jump to the start / end of the line
Ctrl+W Delete the word before the cursor
Ctrl+U Delete everything before the cursor
Ctrl+L Clear the screen (keeps what you're typing)

These come from Emacs-style line editing, which is the default in bash (readline) and zsh (zle). They also work in many other prompts: python3, psql, mysql, node.

9. Ctrl+Z + fg: pause and resume

Editing in vim, or tailing logs, and need to run something quickly? Pause the program instead of quitting:

$ vim notes.md
# press Ctrl+Z
[1]+  Stopped    vim notes.md
$ git status
$ fg
# back in vim, exactly where you left off
Enter fullscreen mode Exit fullscreen mode

jobs lists everything you've paused, and fg %2 brings back a specific one.

10. alias: your own shortcuts

alias gs='git status --short'
alias gl='git log --oneline -10'
alias dcu='docker compose up -d'
Enter fullscreen mode Exit fullscreen mode

Put these in ~/.zshrc (or ~/.bashrc for bash), reload with source ~/.zshrc, and type gs. Run alias with no arguments to see every alias you have.

Cheat sheet

  • Ctrl+R: search history
  • sudo !!: rerun the last command with sudo
  • !$: last argument of the previous command
  • cd -: previous folder
  • Ctrl+A / Ctrl+E: start / end of line
  • Ctrl+W / Ctrl+U: delete word / everything before the cursor
  • Ctrl+L: clear the screen
  • Ctrl+Z + fg: pause and resume
  • alias: your own shortcuts

Try one new shortcut a day this week. What's your favorite terminal trick? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)