Terminal Tips and Tricks for macOS and Linux
Terminal Tips and Tricks for macOS and Linux
The terminal is one of the most powerful tools available to developers and power users. Whether you're on macOS or Linux, these tips will help you move faster, reduce repetition, and get more out of your shell.
Shell Basics Worth Knowing
Navigate Faster
Most people use cd to move around, but a few lesser-known tricks save a lot of keystrokes:
cd - # Jump back to the previous directory
cd ~ # Go to your home directory
pushd /some/path # Push current dir onto a stack, jump to new path
popd # Pop back to the previous stacked directory
Jump to Recent Directories with z
z (or zoxide) is a smarter cd that learns your habits:
brew install zoxide # macOS
# or
sudo apt install zoxide # Linux (Debian/Ubuntu)
Add to your shell config:
eval "$(zoxide init bash)" # or zsh
Now just type z proj to jump to your most-used project directory - no full path needed.
Keyboard Shortcuts That Matter
These work in bash and zsh:
| Shortcut | Action |
|---|---|
Ctrl+A |
Jump to start of line |
Ctrl+E |
Jump to end of line |
Ctrl+W |
Delete word before cursor |
Ctrl+U |
Clear entire line |
Ctrl+R |
Search command history |
Ctrl+L |
Clear the screen |
!! |
Repeat last command |
!$ |
Last argument of previous command |
Aliases: Your Time-Savers
Aliases let you create short commands for things you type constantly. Add these to your ~/.zshrc or ~/.bashrc:
# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ll='ls -lah'
alias la='ls -A'
# Git shortcuts
alias gs='git status'
alias ga='git add .'
alias gc='git commit -m'
alias gp='git push'
alias gl='git log --oneline --graph --decorate'
# Safety nets
alias rm='rm -i' # Prompt before deleting
alias cp='cp -i'
alias mv='mv -i'
# Utilities
alias reload='source ~/.zshrc'
alias ip='curl -s ifconfig.me'
alias ports='lsof -i -P -n | grep LISTEN'
After editing your config, reload it:
source ~/.zshrc
Functions for More Complex Tasks
When an alias isn't enough, use a shell function:
# Create a directory and immediately cd into it
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Quick backup of a file
backup() {
cp "$1" "$1.bak.$(date +%Y%m%d)"
}
# Extract any archive type
extract() {
case "$1" in
*.tar.gz|*.tgz) tar xzf "$1" ;;
*.tar.bz2) tar xjf "$1" ;;
*.zip) unzip "$1" ;;
*.gz) gunzip "$1" ;;
*.rar) unrar x "$1" ;;
*) echo "Unknown archive type: $1" ;;
esac
}
Useful One-Liners
Find and Replace Text in Files
# Replace "foo" with "bar" in all .txt files
find . -name "*.txt" -exec sed -i '' 's/foo/bar/g' {} \;
# On Linux, drop the '' after -i:
find . -name "*.txt" -exec sed -i 's/foo/bar/g' {} \;
Show Disk Usage at a Glance
df -h # Disk usage per partition
du -sh * # Size of each item in current directory
du -sh /* 2>/dev/null | sort -h # Sort by size
Kill a Process Using a Port
# Find what's using port 3000
lsof -i :3000
# Kill it
kill -9 $(lsof -t -i:3000)
Watch a Command Repeatedly
watch -n 2 'df -h' # Run df -h every 2 seconds
Count Files in a Directory
ls -1 | wc -l
History Tips
Your command history is a goldmine. Make the most of it:
# Search history interactively (Ctrl+R does this too)
history | grep "docker"
# Run a command from history by number
!42
# Increase history size in .zshrc or .bashrc
HISTSIZE=10000
HISTFILESIZE=20000
# Avoid duplicate entries
HISTCONTROL=ignoredups:erasedups
macOS-Specific Tips
Use pbcopy and pbpaste
Copy command output to the clipboard:
cat file.txt | pbcopy
pwd | pbcopy
Paste from clipboard into a file:
pbpaste > output.txt
Open Files and URLs from the Terminal
open . # Open current folder in Finder
open file.pdf # Open with default app
open https://example.com # Open in default browser
open -a "Visual Studio Code" . # Open with a specific app
Quick Look from the Terminal
qlmanage -p image.png # Preview a file with Quick Look
Linux-Specific Tips
System Monitoring
htop # Interactive process viewer (install with brew/apt)
iotop # Disk I/O by process
nethogs # Network usage by process
systemd Service Management
sudo systemctl status nginx # Check service status
sudo systemctl start nginx # Start a service
sudo systemctl enable nginx # Enable on boot
sudo journalctl -fu nginx # Follow service logs
Customizing Your Prompt
A good prompt shows you what you need at a glance. For zsh, consider Starship - a fast, cross-shell prompt:
brew install starship
Add to the end of your ~/.zshrc:
eval "$(starship init zsh)"
Starship automatically shows your git branch, Python version, Node version, and more - only when relevant.
Tools Worth Installing
These aren't built-in but are worth adding:
ripgrep(rg) - Much faster thangrepfd- Faster and friendlier thanfindbat-catwith syntax highlightingeza- A modern replacement forlsfzf- Fuzzy finder for files and historytldr- Simplified man pages
Install them all at once on macOS:
brew install ripgrep fd bat eza fzf tldr
On Linux (Debian/Ubuntu):
sudo apt install ripgrep fd-find bat fzf
The terminal rewards the time you put into learning it. Start with a few aliases and one or two new tools, get comfortable, then build from there.