Introduction
Mastering Bash shortcuts transforms the terminal from a place you type slowly into a place you work fast. These shortcuts are built into GNU Readline — the library that powers Bash’s line editing — and work in any Readline-enabled program (Python REPL, psql, irb, etc.).
Cursor Movement
Stop tapping arrow keys one character at a time:
| Shortcut | Action |
|---|---|
Ctrl+A |
Move to beginning of line |
Ctrl+E |
Move to end of line |
Alt+B |
Move back one word |
Alt+F |
Move forward one word |
Ctrl+XX |
Toggle between current position and beginning of line |
# Example: you typed a long command and need to fix the beginning
$ sudo apt install nginx curl wget vim git # cursor at end
# Ctrl+A → cursor jumps to beginning
# Alt+F → jump word by word to find the typo
Editing
| Shortcut | Action |
|---|---|
Ctrl+U |
Cut from cursor to beginning of line |
Ctrl+K |
Cut from cursor to end of line |
Ctrl+W |
Cut the word before the cursor |
Alt+D |
Cut the word after the cursor |
Ctrl+Y |
Paste (yank) the last cut text |
Ctrl+_ |
Undo last edit |
Ctrl+T |
Swap current character with previous |
Alt+T |
Swap current word with previous |
Alt+U |
Uppercase from cursor to end of word |
Alt+L |
Lowercase from cursor to end of word |
Alt+C |
Capitalize current word |
# Ctrl+W is incredibly useful for fixing typos
$ git comit -m "fix bug"
# ↑ cursor here, Ctrl+W deletes "comit", type "commit"
# Ctrl+U + Ctrl+Y: cut and paste the whole line
$ sudo very-long-command --with --many --flags
# Ctrl+U cuts everything, type "time ", Ctrl+Y pastes it back
$ time sudo very-long-command --with --many --flags
Command History
| Shortcut | Action |
|---|---|
Ctrl+R |
Reverse search through history |
Ctrl+S |
Forward search through history |
Ctrl+P |
Previous command (same as Up arrow) |
Ctrl+N |
Next command (same as Down arrow) |
Ctrl+G |
Cancel history search |
!! |
Repeat last command |
!$ |
Last argument of previous command |
!* |
All arguments of previous command |
!string |
Last command starting with “string” |
!?string |
Last command containing “string” |
^old^new |
Replace “old” with “new” in last command |
# Ctrl+R is the most powerful history shortcut
$ [Ctrl+R]
(reverse-i-search)`': _
# Type any part of a previous command to find it
(reverse-i-search)`docker': docker run -it ubuntu bash
# !! — repeat last command (great for sudo)
$ apt update
Permission denied
$ sudo !!
$ sudo apt update
# !$ — reuse last argument
$ mkdir -p /very/long/path/to/directory
$ cd !$
# cd /very/long/path/to/directory
# ^old^new — fix a typo in the last command
$ git push origin mian
$ ^mian^main
# git push origin main
History Configuration
Add to ~/.bashrc for better history:
# Larger history
export HISTSIZE=10000
export HISTFILESIZE=20000
# Don't save duplicates or commands starting with space
export HISTCONTROL=ignoreboth
# Append to history instead of overwriting
shopt -s histappend
# Save timestamp with each command
export HISTTIMEFORMAT="%F %T "
# Immediately write to history file
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
Process Control
| Shortcut | Action |
|---|---|
Ctrl+C |
Kill foreground process (SIGINT) |
Ctrl+Z |
Suspend foreground process (SIGTSTP) |
Ctrl+D |
Send EOF / exit shell |
Ctrl+L |
Clear screen (same as clear) |
Ctrl+S |
Pause terminal output (XOFF) |
Ctrl+Q |
Resume terminal output (XON) |
# Ctrl+Z + bg: run a process in the background
$ vim large-file.txt
[Ctrl+Z]
[1]+ Stopped vim large-file.txt
$ bg
[1]+ vim large-file.txt &
# fg: bring back to foreground
$ fg
# jobs: list background jobs
$ jobs
[1]+ Running vim large-file.txt &
Tab Completion
Tab completion is one of Bash’s most powerful features:
# Complete commands
$ git [Tab][Tab]
add bisect branch checkout clone commit ...
# Complete file paths
$ cat /etc/ssh/[Tab]
ssh_config sshd_config ssh_host_rsa_key ...
# Complete variables
$ echo $HO[Tab]
$HOME $HOSTNAME
# Install bash-completion for enhanced completions
sudo apt install bash-completion
# Then add to ~/.bashrc:
source /etc/bash_completion
Useful Shell Tricks
Brace Expansion
# Create multiple directories at once
mkdir -p project/{src,tests,docs,scripts}
# Create multiple files
touch app.{js,css,html}
# Backup a file
cp config.yaml{,.bak} # creates config.yaml.bak
# Rename (move) a file
mv server.{js,ts} # renames server.js to server.ts
Command Substitution
# Use output of a command as an argument
echo "Today is $(date +%Y-%m-%d)"
cd $(git rev-parse --show-toplevel) # go to git root
# Assign to variable
current_branch=$(git branch --show-current)
echo "On branch: $current_branch"
Here Documents
# Write multi-line content to a file
cat > config.yaml << 'EOF'
database:
host: localhost
port: 5432
name: myapp
EOF
Quick Directory Navigation
# cd - : go to previous directory
$ cd /var/log
$ cd /etc/nginx
$ cd -
/var/log
# pushd/popd: directory stack
$ pushd /var/log
$ pushd /etc/nginx
$ dirs
/etc/nginx /var/log ~
$ popd
/var/log
$ popd
~
fzf: Fuzzy Finder (Game Changer)
fzf supercharges history search and file navigation:
# Install
sudo apt install fzf
# or
brew install fzf
# After installation, add to ~/.bashrc:
eval "$(fzf --bash)"
# Now Ctrl+R opens an interactive fuzzy history search
# Ctrl+T opens fuzzy file finder
# Alt+C opens fuzzy directory changer
Useful Aliases
Add to ~/.bashrc:
# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias ~='cd ~'
# Listing
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Safety
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Git shortcuts
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph --decorate'
# Quick edit config files
alias bashrc='$EDITOR ~/.bashrc && source ~/.bashrc'
alias vimrc='$EDITOR ~/.vimrc'
# Reload shell config
alias reload='source ~/.bashrc'
Quick Reference Card
MOVEMENT EDITING HISTORY
Ctrl+A line start Ctrl+U cut to start Ctrl+R reverse search
Ctrl+E line end Ctrl+K cut to end !! last command
Alt+B word back Ctrl+W cut word back !$ last argument
Alt+F word fwd Ctrl+Y paste ^a^b fix typo
PROCESS COMPLETION TRICKS
Ctrl+C kill Tab complete !! sudo !!
Ctrl+Z suspend Tab Tab list all !$ reuse arg
Ctrl+D exit/EOF Alt+? show options ^x^y fix typo
Ctrl+L clear screen
Comments