mtnr

A tech blog with fries on the side

5 Terminal Tricks That Will Save You Time Every Day

Whether you’re on macOS or Linux, your shell is hiding some surprisingly powerful shortcuts. Here are five tricks — spanning both zsh and bash — that are easy to learn and genuinely useful.


1. !$ and $_ — Reuse the Last Argument

How often do you run a command and immediately need to use the same argument again?

mkdir -p /some/deeply/nested/directory
cd !$

!$ expands to the last argument of the previous command, so you don’t have to type that long path twice. In interactive shells you can also use $_, which does the same thing but works more reliably inside scripts.

Note: !$ is a history expansion — it’s evaluated before the command runs and will be visible if you press Tabto expand it first. $_ is a special shell variable set after each command completes.


2. ^old^new — Quick Command Substitution

Made a typo in the last command? Instead of pressing  and hunting for the mistake, use caret substitution:

git comit -m "fix typo"
^comit^commit
# Runs: git commit -m "fix typo"

This replaces the first occurrence of old with new in the previous command and re-runs it. It’s a bash and zsh history expansion, so it works in both.


3. cd - — Jump Back to the Previous Directory

cd - switches you to whichever directory you were in before the current one. It’s like a back button for your filesystem.

cd /var/log
cd /etc/nginx
cd -         # Back to /var/log
cd -         # Back to /etc/nginx

zsh takes this a step further with a built-in directory stack. Running cd - followed by Tab in zsh gives you a numbered list of recent directories to jump to directly — handy when you’re hopping between more than two locations.


4. pbcopy / pbpaste (macOS) and xclip / xsel (Linux)

These commands pipe data into and out of your system clipboard, which is incredibly useful for moving output between the terminal and other apps.

macOS:

cat ~/.ssh/id_ed25519.pub | pbcopy   # Copy your public key to clipboard
pbpaste > notes.txt                  # Paste clipboard content into a file

Linux (install xclip first):

cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
xclip -selection clipboard -o > notes.txt

A convenient alias to make Linux feel like macOS:

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

5. Background Jobs — Don’t Let Long Tasks Hold Your Terminal Hostage

Running a long process? You don’t have to open a new terminal tab. The shell has built-in job control for exactly this.

Send a running process to the background:

  1. Press Ctrl+Z to suspend it.
  2. Run bg to resume it in the background.

Start a process directly in the background:

npm run build &

Manage your jobs:

jobs          # List all background jobs
fg            # Bring the most recent job back to the foreground
fg %2         # Bring job number 2 to the foreground

Heads up: A backgrounded job is still tied to your terminal session. If you close it, the process gets killed. To truly detach a process so it survives after you log out, use nohup:

nohup ./my-script.sh &

Or reach for tmux or screen for a more complete session management experience.


These shortcuts take only minutes to learn but add up to real time savings over a day of terminal work. Try adding the pbcopy/pbpaste aliases to your .zshrc or .bashrc today — your future self will thank you.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *