He sendeth forth his commandment upon earth: his word runneth very swiftly.
Psalms 147:15, The Bible
Poem: Bourne Again
I'm born again like Matt Murdock โ Transformed when I use Bash; Performing signs and miracles, My fingers flinging fast ๐จโ๐ป.
Bash and the terminal are two of the best things to happen to computing. I can talk to the soul of the computer with just the tips of my fingers โ talk about sheer power! It's the OG ChatGPT ๐.
In this blog post, I'd like to talk about Bash1 shortcuts that save me typing time.
Shell shortcuts
Rename a file using brace expansion ({}
):
$ mv imaginary-playmate{,-rene-and-angela}.mp3
mv imaginary-playmate.mp3 imaginary-playmate-rene-and-angela.mp3
Run previous command with sudo
using history expansion (!
):
$ rm -rf / # Delete folder without proper permission
Permission denied.
$ sudo !! # Run last command with "sudo"
sudo rm -rf /
Run last command with a specific prefix:
$ ssh user@mmhq.me # SSH to server
# Run a couple commands on server ...
# ...
$ !ssh # Return to server
ssh user@mmhq.me
Print last command (so you know what it is before you run it):
$ !make:p # print last "make" command without running it
make VERBOSE=1
# Press the "Up" key and hit "Enter" to run the command.
Copy folder and all its contents:
$ cp ~/Music/songs/ /run/media/flashdrive/ # Try to copy folders with the "-r" flag missing, fails
cp: -r not specified; omitting directory '/home/efe/Music/songs/'
$ cp -r !* # Copy folder with the right flag, succeeds
cp -r ~/Music/songs/ /run/media/flashdrive/
Run a very long command; use the printed result in another command:
$ ls -1rth ~/Music/songs/*.mp3 | tail -1 # Print last added song in folder
/home/efe/Music/songs/imaginary-playmate.mp3
$ mid3v2 -l $(!!) # Check ID3 metadata tag in MP3 file
mid3v2 -l $(ls -1rth ~/Music/songs/*.mp3 | tail -1)
Use last argument of last command:
$ cp blog/posts/.template.md blog/posts/something-exciting-i-have-to-say.md # Create new blog post by copying template file
$ vim !$ # Open Markdown file in vim
vim blog/posts/something-exciting-i-have-to-say.md
Check the error status of the last command to know if I should run it again:
$ yay -Syu # Run updates on my computer
$ echo $? # Print last error code; prints "0" if successful and any other number if not
0
Switch directories without typing the directory names:
$ cd ~/Downloads # Switch to "Downloads" directory
$ cd ~/Documents # Switch to "Documents" directory
$ cd - # Switch back to "Downloads" directory
$ cd # Switch to home directory
Run a recently run command:
$ history | grep rm # Check history for all "rm" commands
1094 rm -rf blog/posts/talk-a-lot.md
1096 rm -rf /tmp/cache
1099 rm -rf blog/posts/talk-some-more.md
$ !1096 # Run second-to-last command
rm -rf /tmp/cache
Keyboard shortcuts
- Ctrl + W to cut word
- Ctrl + Y to restore cut words
- โ to display previous command
- Ctrl + D to logout and close Linux terminal (or to make Mac terminal stop responding and force you to close it and reopen it ๐๐คฆ).
- Ctrl + C to kill a process that's taking forever, or when I run a script or program by mistake.
- Ctrl + L to clear the screen.
-
Ctrl + Z to suspend Vim. (Restore Vim with the
fg
command.) - Ctrl + A to jump to the start of the line.
- Ctrl + E to jump to the end of the line.
- Ctrl + R to reverse search through command history.
- Tab to autocomplete commands and file paths.
- Alt + . to display last argument of the last command.
Bottom line
You don't have to move like a snail ๐ when using the terminal. Try these shortcuts out today and become the Speedy Gonzalez โก of executing commands. (Leave your nerdy coworkers in awe ๐ค๐.)
Further reading
-
Bash stands for Bourne-Again Shell. Yes, I will see it in heaven ๐๐๐.ย โฉ