Shell Tricks That Make Life Easier (and Save Your Sanity)
This post dives into essential, sanity-saving shell tricks that elevate your command-line efficiency from basic navigation to power-user wizardry. It covers universal POSIX-ish commands and Bash/Zsh specific features, from quick line editing to robust scripting safeguards. Hacker News readers, who frequently live in the terminal, appreciate these practical insights that promise to streamline workflows and banish common frustrations.
The Lowdown
The article addresses the widespread inefficiency of many engineers who, despite spending significant time in the terminal, never progress beyond basic commands like ls and cd. It highlights how powerful, yet often unknown, shell features from the 1980s can drastically improve productivity and reduce frustration. The author categorizes these tricks into universally applicable commands and those specific to modern interactive shells like Bash and Zsh.
- Universal Shell Tricks (POSIX-ish)
- Line Editing:
CTRL + W(delete word),CTRL + U(cut to beginning),CTRL + K(cut to end),CTRL + Y(paste),CTRL + A(start of line),CTRL + E(end of line),ALT + B/ALT + F(word navigation). - Terminal Recovery:
resetorstty saneto fix corrupted terminal output. - Emergency Exits:
CTRL + C(cancel command),CTRL + D(EOF/logout). - Screen Cleaner:
CTRL + Lto clear the terminal without interrupting input. - Directory Navigation:
cd -(toggle previous directory),pushd/popd(directory stack management). - File Manipulation:
> file.txt(truncate file),$_(last argument of previous command). - Scripting Sanity:
set -e(exit on error),set -u(error on unset variables).
- Line Editing:
- Bash & Zsh Specific Tricks (Comfort Zone)
- History Search:
CTRL + R(reverse incremental search). - Command Reuse:
!!(repeat previous command, e.g., withsudo). - Editor Integration:
CTRL + X,CTRL + E(edit command in external editor),fc(edit previous command in external editor). - Last Argument:
ESC + .orALT + .(insert last argument interactively),!$(non-interactive last argument). - Brace Expansion:
cp file{,.bak},mv file.{txt,md},mkdir -p project/{src,tests,docs}. - Process Substitution:
diff <(sort file1) <(sort file2). - Globstar:
**for recursive file matching (withshopt -s globstarin Bash). - Backgrounding/Disowning:
CTRL + Z,bg,disownto detach processes from the terminal. - Everything-Logger:
command |& tee file.log(pipe stdout and stderr totee).
- History Search:
The article concludes by encouraging users to adopt these tricks incrementally, suggesting they pick one to practice for a week before moving to another, ultimately transforming the shell from an obstacle course into a powerful, personalized toolbox.