SH

Shortcuts & Productivity

Bash & Linux · 9 entries

Reverse History Search

syntax
Ctrl+R, then type to search
example
# Press Ctrl+R, then type 'docker'
(reverse-i-search)`docker': docker compose up -d --build

Note Press Ctrl+R again to cycle through older matches. Enter executes the found command. Ctrl+G or Esc cancels the search. This is one of the biggest timesavers in daily terminal work.

History Expansion: !! and !$

syntax
!!        # last command
!$        # last argument of previous command
!n        # command number n
!string   # last command starting with string
example
apt install nginx
sudo !!
# Expands to: sudo apt install nginx

mkdir /opt/new-app
cd !$
# Expands to: cd /opt/new-app

!grep
# Reruns last command that started with 'grep'

Note !! is indispensable when you forget sudo. !$ avoids retyping long paths. Use !:n to reference a specific argument (e.g., !:2 for the second). Add :p to preview without executing: !!:p shows what would run.

Create Command Aliases

syntax
alias name='command'
unalias name
example
alias ll='ls -lah'
alias gs='git status'
alias dc='docker compose'
alias k='kubectl'
alias myip='curl -s ifconfig.me'

Note Aliases defined in the terminal are lost when the shell exits. Add them to ~/.bashrc or ~/.zshrc to make them permanent. Run alias with no arguments to list all current aliases. Use unalias to remove one.

Shell Configuration Files

syntax
~/.bashrc  (Bash interactive)
~/.zshrc   (Zsh interactive)
~/.profile (login shells)
example
# Add to ~/.bashrc or ~/.zshrc:
export PATH="$HOME/.local/bin:$PATH"
export EDITOR=vim
alias deploy='cd ~/projects/main && ./deploy.sh'

# Reload after editing:
source ~/.bashrc

Note .bashrc runs for every new interactive Bash shell. .bash_profile runs only for login shells. .zshrc runs for every interactive Zsh shell. After editing, run source ~/.bashrc (or source ~/.zshrc) to apply changes without opening a new terminal.

Brace Expansion

syntax
{a,b,c}
{start..end}
{start..end..step}
example
mkdir -p project/{src,tests,docs}
cp config.yml{,.bak}
echo {1..10}
echo {01..12}
touch report_{Q1,Q2,Q3,Q4}_2026.pdf
output
1 2 3 4 5 6 7 8 9 10
01 02 03 04 05 06 07 08 09 10 11 12

Note config.yml{,.bak} expands to 'config.yml config.yml.bak', making a quick backup. Brace expansion happens before variable expansion, so you cannot use variables inside braces. Ranges support zero-padding ({01..12}) and steps ({0..20..5}).

Command Substitution

syntax
$(command)
`command` (legacy)
example
echo "Today is $(date '+%A, %B %d')"
BRANCH=$(git rev-parse --abbrev-ref HEAD)
FILES_CHANGED=$(git diff --name-only | wc -l)
echo "${FILES_CHANGED} files changed on ${BRANCH}"
output
Today is Saturday, April 04
3 files changed on feature/auth

Note Always use $() instead of backticks. $() nests cleanly: $(echo $(whoami)) works; backticks require escaping for nesting. The output has trailing newlines stripped automatically.

CDPATH: Quick Directory Navigation

syntax
export CDPATH=.:dir1:dir2
example
# Add to ~/.bashrc or ~/.zshrc:
export CDPATH=".:$HOME/projects:$HOME/work"

# Now from anywhere:
cd web-app
# Jumps to ~/projects/web-app without typing the full path

Note CDPATH tells cd to search additional base directories. The leading . ensures the current directory is still checked first. Separate paths with colons. This drastically reduces typing for developers who frequently switch between project directories.

Essential Keyboard Shortcuts

syntax
Ctrl+A / Ctrl+E
Ctrl+U / Ctrl+K
Ctrl+W
Ctrl+L
Ctrl+C / Ctrl+D
example
Ctrl+Amove cursor to beginning of line
Ctrl+Emove cursor to end of line
Ctrl+Udelete from cursor to beginning
Ctrl+Kdelete from cursor to end
Ctrl+Wdelete previous word
Ctrl+Lclear screen (same as clear)
Ctrl+Ccancel current command
Ctrl+Dexit shell (or send EOF)
Ctrl+Zsuspend foreground process
Alt+.   → insert last argument from previous command

Note These work in both Bash and Zsh and are based on Emacs keybindings (the default). Alt+. is one of the most underused shortcuts; it cycles through previous commands' last arguments. Use set -o vi in your shell config to switch to Vim-style keybindings.

Tab Completion

syntax
Tab       # complete
Tab Tab   # show all possibilities
example
cd /etc/ng<Tab>
# Completes to: cd /etc/nginx/

git che<Tab><Tab>
# Shows: checkout  cherry  cherry-pick

Note Tab completion works for commands, file paths, and (with bash-completion or zsh plugins) for subcommands and flags of many tools including git, docker, kubectl, and ssh hosts. Install bash-completion for enhanced support in Bash.