DEV Community

AttractivePenguin
AttractivePenguin

Posted on

Linux Shell Shortcuts That Will Save You Hours Every Week

Linux Shell Shortcuts That Will Save You Hours Every Week

If you spend any significant time in the terminal, you've probably wondered if there's a faster way to do things. Spoiler: there absolutely is. Most developers use only a fraction of their shell's capabilities, leaving serious productivity gains on the table.

Let me show you the keyboard shortcuts, history tricks, and workflow optimizations that collectively save me hours every week. These work in both bash and zsh, so whether you're on Ubuntu, macOS, or a Raspberry Pi, you're covered.

Why Shell Shortcuts Matter

Before diving into specific shortcuts, let's talk about why this matters. The average developer runs hundreds of commands daily. If each command takes even 5 seconds longer than necessary due to retyping, scrolling through history manually, or losing your place—those seconds add up to real hours.

Shell shortcuts aren't just about speed. They reduce cognitive load. When you can recall a complex command instantly instead of reconstructing it from memory, you keep your focus on the problem you're solving, not the mechanics of your tools.

The Essentials: Navigation Shortcuts

Let's start with the fundamentals that you'll use constantly.

Moving Around the Command Line

Shortcut Action
Ctrl + A Jump to beginning of line
Ctrl + E Jump to end of line
Ctrl + B Move back one character (←)
Ctrl + F Move forward one character (→)
Alt + B Move back one word
Alt + F Move forward one word

These seem simple, but here's the key insight: keeping your hands on the home row is faster than reaching for arrow keys. Once these become muscle memory, you'll navigate commands at the speed of thought.

# Example: You typed a long command but need to fix the first flag
$ docker run --name myapp -p 8080:80 -v /data:/app/data -e NODE_ENV=production node:18

# Instead of holding ← for 10 seconds, press Ctrl+A and you're there instantly
# Then Alt+F to jump by words to the exact spot
Enter fullscreen mode Exit fullscreen mode

Editing on the Line

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 cursor
Ctrl + Y Paste (yank) last cut text
Ctrl + _ Undo last edit

The cut and paste shortcuts are incredibly powerful. Cut text with Ctrl+U or Ctrl+K, and it goes into a clipboard-like buffer. Ctrl+Y brings it back.

# You're editing a command and want to try a different flag
$ kubectl get pods -n production --field-selector=status.phase=Running

# Press Ctrl+W to remove "Running", type "Pending" instead
$ kubectl get pods -n production --field-selector=status.phase=Pending

# Need the original back? Ctrl+_ undoes the change
Enter fullscreen mode Exit fullscreen mode

History Search: The Game Changer

This is the single most impactful shortcut in my toolkit.

Reverse Incremental Search

Press Ctrl + R and start typing. Your shell searches backward through your history, showing matches in real-time.

$ # Press Ctrl+R
(reverse-i-search)``: 
$ # Start typing 'docker'
(reverse-i-search)`docker': docker-compose -f docker-compose.prod.yml up -d
$ # Press Ctrl+R again to cycle through older matches
(reverse-i-search)`docker': docker exec -it mycontainer bash
Enter fullscreen mode Exit fullscreen mode

Press Enter to execute the found command, or Ctrl+C to cancel.

Pro tip: In zsh with the excellent Oh My Zsh framework, you can use the up/down arrows after a partial command to search history matching what you've typed:

$ docker  # Press ↑ to cycle through all docker commands in history
Enter fullscreen mode Exit fullscreen mode

History Expansion Shortcuts

Shortcut Action
!! Repeat last command
!$ Last argument of previous command
!* All arguments of previous command
!n Command number n in history
!pattern Last command starting with pattern

These look cryptic but are incredibly useful:

$ ls /very/long/directory/path/that/took/forever/to/type
$ cd !$    # Expands to: cd /very/long/directory/path/that/took/forever/to/type

$ apt update
Permission denied
$ sudo !!   # Repeats last command with sudo: sudo apt update

$ git commit -m "Fix bug" && git push origin main
$ !*        # Would expand to: commit -m "Fix bug" && git push origin main
Enter fullscreen mode Exit fullscreen mode

The Alt + . shortcut is my personal favorite—it inserts the last argument from the previous command:

$ mkdir -p ~/projects/awesome-new-project/src/components
$ cd Alt+.   # Expands to: cd ~/projects/awesome-new-project/src/components
Enter fullscreen mode Exit fullscreen mode

You can press Alt + . repeatedly to cycle through previous commands' last arguments.

Process Control Shortcuts

Shortcut Action
Ctrl + C Send SIGINT (interrupt/kill current process)
Ctrl + Z Suspend current process
Ctrl + D Send EOF (close shell/logout)
Ctrl + L Clear screen (faster than typing clear)

The suspension with Ctrl+Z is particularly powerful when combined with job control:

$ vim config.yaml          # Editing a file
# Press Ctrl+Z
[1]+  Stopped                 vim config.yaml
$ grep "error" /var/log/app.log   # Run another command
$ fg                       # Return to vim (foreground)
Enter fullscreen mode Exit fullscreen mode

Use jobs to see suspended processes, fg %1 to bring job 1 to foreground, bg %1 to run job 1 in background.

Tmux: The Session Manager

If you're not using tmux (or screen), you're missing out on one of the biggest productivity boosters available.

Why Tmux?

  1. Persistent sessions: Your terminals survive SSH disconnections, computer restarts (with configuration), and accidental terminal window closes
  2. Multiple windows: Organize related terminals in one place
  3. Split panes: View multiple terminals simultaneously
  4. Session sharing: Pair program with remote colleagues

Essential Tmux Shortcuts

Tmux uses a prefix key (default Ctrl+B), followed by a command key:

Shortcut Action
Ctrl+B C Create new window
Ctrl+B N Next window
Ctrl+B P Previous window
Ctrl+B % Split pane vertically
Ctrl+B " Split pane horizontally
Ctrl+B D Detach from session
Ctrl+B [ Enter copy mode (scroll)

A Practical Tmux Workflow

# Start a named session for your project
$ tmux new -s myproject

# Inside, create windows for different tasks
Ctrl+B C    # New window - maybe for running the dev server
Ctrl+B C    # Another window - for git operations
Ctrl+B 2    # Jump to window 2

# Split a window for side-by-side viewing
Ctrl+B %    # Vertical split - now you have two panes
Ctrl+B "    # Horizontal split in one pane

# When done, detach (session keeps running)
Ctrl+B D

# Later, reattach
$ tmux attach -t myproject

# List sessions
$ tmux ls
Enter fullscreen mode Exit fullscreen mode

Detaching Saves the Day

# You're running a long database migration on a remote server
$ ./migrate-production-db.sh
# Suddenly your VPN drops
# With tmux, the process continues
# Reconnect, then:
$ tmux attach -t migration
# You're right where you left off
Enter fullscreen mode Exit fullscreen mode

Real-World Scenarios

Scenario 1: Debugging a Container

You're debugging why a Docker container keeps crashing:

# Run the container
$ docker run -d --name debug-container myapp:latest

# It crashes. Check logs
$ docker logs debug-container
Error: Database connection failed

# You need to tweak the connection string
# Instead of retyping everything:
$ docker run -d --name debug-container -e DB_HOST=10.0.0.5 myapp:latest
# Oops, need to remove the old container first
$ docker rm -f debug-container && Alt+. Alt+.  # Reuses arguments cleverly

# Or use history search
Ctrl+R, type 'docker run', find the command, edit it
Enter fullscreen mode Exit fullscreen mode

Scenario 2: Long-Running Process on Remote Server

You need to run a data processing script on a production server that takes hours:

# SSH into server
$ ssh production-server

# Start tmux first!
$ tmux new -s data-process

# Run your script
$ python3 process_large_dataset.py --input /data/raw --output /data/processed

# Detach when you need to step away
Ctrl+B D

# You can even close your laptop, go home
# Later, from home:
$ ssh production-server
$ tmux attach -t data-process
# Process is still running, output visible
Enter fullscreen mode Exit fullscreen mode

Scenario 3: Fixing a Typo Mid-Command

$ kubectrl get pods -n kube-system   # Typo: 'kubectrl' instead of 'kubectl'

# Don't delete and retype! Use navigation
Ctrl+A    # Go to beginning
Alt+F     # Jump forward one word (now at 'get')
Alt+F     # Jump to 'pods'
Alt+F     # Jump to '-n'
Alt+F     # Jump to 'kube-system'
Ctrl+B Ctrl+B  # Back up two characters to the 'l' in kubectrl
# Delete 'r' and 'l', add 'l' and 'e' to fix
Enter fullscreen mode Exit fullscreen mode

Troubleshooting FAQ

Q: These shortcuts aren't working on my system!

A: Check your shell. Run echo $0 or ps -p $$. If you're not using bash or zsh, these shortcuts may differ. On some systems, Alt is Option (macOS) or Esc (press and release, then the key).

For zsh, ensure you're using Emacs keybindings (the default):

bindkey -e  # Emacs mode
bindkey -v  # Vi mode (different shortcuts)
Enter fullscreen mode Exit fullscreen mode

Q: My history isn't being saved between sessions!

A: Add these to your ~/.bashrc or ~/.zshrc:

# Ignore duplicates and commands starting with space
export HISTCONTROL=ignoredups:ignorespace
# Increase history size
export HISTSIZE=10000
export HISTFILESIZE=20000
# Append to history file, don't overwrite
shopt -s histappend  # bash only
Enter fullscreen mode Exit fullscreen mode

Q: Tmux says "no server running" but I had a session!

A: If your system rebooted, tmux sessions are lost (they're stored in memory, not persisted to disk). For persistence across reboots, consider:

  1. Using tmux plugins like tmux-resurrect and tmux-continuum (via TPM)
  2. Running critical processes as systemd services or in Docker containers
  3. Using terminal multiplexing tools designed for persistence

Q: Can I customize these shortcuts?

A: Absolutely! In bash, use the bind command. In zsh, use bindkey:

# Make Ctrl+Shift+R search forward (in addition to Ctrl+R backward)
bindkey '^R' history-incremental-search-backward
bindkey -s '^S' '!!\n'  # Ctrl+S repeats last command
Enter fullscreen mode Exit fullscreen mode

Conclusion

Shell shortcuts aren't flashy. They won't impress anyone at a demo. But they're the compound interest of developer productivity: small gains, applied thousands of times, accumulating into serious time savings.

Start small. Pick three shortcuts—Ctrl+R, Alt+., and Ctrl+A/Ctrl+E—and commit to using them for a week. Once they become automatic, add more. Consider setting up tmux for your next project.

The terminal is where developers live. Make yourself at home.

Top comments (0)