The command line is a powerful tool, but it can be even more efficient if you know how to navigate it quickly. These terminal tricks will help you work faster and smarter in your shell.
Essential Ctrl Key Shortcuts
These shortcuts are fundamental for anyone who spends time in the terminal. They help you move around and edit commands with ease.
Ctrl + a: Move to the beginning of the line.Ctrl + e: Move to the end of the line.Ctrl + u: Cut the line before the cursor.Ctrl + k: Cut the line after the cursor.Ctrl + y: Paste the last cut text.Ctrl + w: Cut the word before the cursor.Ctrl + l: Clear the screen, equivalent to theclearcommand.Ctrl + r: Search backward through your command history.
Navigating Your Command History
Quickly find and re-run previous commands without retyping them.
!!: Execute the last command.sudo !!: Execute the last command withsudo.!cat: Execute the last command that started withcat.!53: Execute the 53rd command in your history.
Quick Command Corrections
Made a typo? No need to retype the whole command.
^aaa^bbb: Replaces the first occurrence ofaaawithbbbin the last command and executes it.!!:gs/aaa/bbb/: Replaces all occurrences ofaaawithbbbin the last command and executes it.
Bash Globbing for File Matching
Use wildcards to work with multiple files at once.
*: Matches any number of characters.?: Matches any single character.[]: Matches any character within the brackets (e.g.,[a-z]).{}: Matches a comma-separated list of patterns (e.g.,*.{sh,py}).
Conclusion
Mastering these terminal tricks will significantly improve your command-line productivity. Start by incorporating a few into your daily workflow, and soon they’ll become second nature.