If you've always wanted to become one of those cool hackers on TV, this post is for you. I'm going to teach you how to become lightning fast and efficient in the Linux terminal.
I assume that you've got basic Linux knowledge or at least have used a Linux terminal before.
Basics
It never hurts to go over the basics, right?
Don't stray far from home
See those bumps on your keyboard on the F and J keys? That's where fingers belong. Forget the arrow keys exist. By the end of this post, everything they do lives on the home row. And don't even think about reaching for the mouse.
Keep output minimal
Even though this post is mostly about keeping input minimal, it's always a good idea to keep output minimal as well. Try to make the information on your screen as useful as possible, so you don't have to scroll around endlessly.
- Don't run
ls -laif you don't care about hidden files or the directory itself. - Don't run commands in verbose mode unless necessary. They will wipe all the useful prior output.
- Use grep, awk etc. to filter output
Go slow and never stop learning
Take your time to learn new things. It pays off very quickly. Learn one new thing at a time. You won't remember everything at once.
- Read about a single thing (or two if you're extra smart)
- Use the thing in practice until it becomes second nature
- Learn the next thing
By the way, these are Emacs bindings
Here's something it took me embarrassingly long to learn: the shortcuts below aren't bash inventions. Bash reads your input through a library called readline, and readline speaks Emacs by default.
That's great news. Learn these shortcuts once and they work in far more places than bash: zsh, the Python REPL, psql, and countless other tools use readline or imitate its bindings. Just maybe don't mention the E-word over at r/vim.
Tab / Ctrl + I = Autocomplete
This is the most important one. It is the reason CLIs are more powerful than a GUI and always will be.
Using the Tab key (or better yet: Ctrl + I) lets you autocomplete commands, directories and parameters. You type the beginning of a word and hit Tab to let the shell do the rest.
Not only does this improve typing speed, it also reduces the amount of errors, hence speeding up your work radically.
For an improved completion experience, make sure bash-completion is installed on your machine. If you want to get
even more fancy, try zsh and oh-my-zsh.
In fact, forget the Tab key even exists. Ctrl + I is right there on the home row.
Ctrl + J = Return
You won't ever have to reach for the return key again. Well, unless some annoying TUI program requires you to. But there are quite a few programs that accept this shortcut.
Ctrl + A/E to go to the beginning/end of the line
Ctrl + A puts your cursor to the beginning (Anfang in German) of the line. Ctrl + E sends your cursor to the end.
Ctrl + B/F to go backward and forward
On regular keyboards the arrow keys are so far away from the home row, you might as well reach for the mouse or for the moon. A more ergonomic option is using Ctrl + B for going one character backward and Ctrl + F for going forward. It takes a bit getting used to, but it's well worth it.
For jumping whole words at a time, use Alt + B and Alt + F. Much faster than crawling character by character.
Ctrl + P/N to cycle through command history
Speaking of the arrow keys: You probably already know that hitting up or down will cycle through the command history and let you reuse commands. The more comfortable option is hitting Ctrl + P to get the previous command and Ctrl + N to get the next one.
Ctrl + H = Backspace
Whoever came up with this one must have made many typing mistakes and I'm glad he did. Once you internalize this shortcut, making mistakes won't feel as painful anymore.
Ctrl + W = Delete a word
If deleting characters one by one is too slow for you, you can delete whole words using Ctrl + W. This deletes the word to the left.
If you feel fancy, you can hit Alt + D to delete a word to the right.
Ctrl + U/K = Delete the line
If the line is unsalvageable, nuke everything left of the cursor with Ctrl + U. Its counterpart Ctrl + K deletes everything from the cursor to the end of the line. Since your cursor usually sits at the end anyway, Ctrl + U in practice means "delete the line".
Ctrl + Y = Paste it back
Ctrl + W, Alt + D, Ctrl + U, and Ctrl + K don't just delete: they cut. The deleted text goes into a "kill ring" and Ctrl + Y pastes it back. This turns delete-and-retype into cut-and-paste without ever leaving the command line.
Ctrl + R to search command history
This one lets you search through your previously executed commands. It takes a while to get used to, but it is extremely useful.
Type Ctrl + R and then whatever command you're looking for. Keep hitting Ctrl + R to go further back through older matches. Ctrl + S searches in the other direction, back toward newer commands.
stty -ixon in your .bashrc to use it for
searching. And if your screen ever appears frozen: Ctrl + Q unfreezes it.Alt + . brings back the last argument
This brings back the last argument from the previous command. Keep hitting it to cycle through the last arguments of earlier commands.
E.g.: Instead of
rm dir -r you type rm -r dir so
the next time you hit Alt + . you get "dir" instead
of a useless "-r".Ctrl + X, Ctrl + E = Edit in your editor
When a command outgrows a single line (loops, long pipelines), hit Ctrl + X followed by Ctrl + E to open the current line in your $EDITOR. Write, save, quit,
and the command runs. Naturally, this is the best shortcut of them all if
your editor is Vim.
Ctrl + L clears the screen
This is your windshield wiper. You don't need to hit Return Ctrl + J a hundred times to get a blank state. Just
hit Ctrl + L once and your screen is clear. In most
shells you can even keep scrolling back, so the output from before isn't
lost.
Ctrl + D = Exit
Never ever type "exit" to leave the terminal. Not only is it a huge waste of time and energy, it also clogs up your history. Just hit Ctrl + D to send an end-of-file and close the shell instantly.
Where to go from here
Your terminal is fast now. Your editor is next: How to become a Vim pro.