These Linux Command Line Shortcuts Will Boost Your Efficiency
Linux & macOS Terminal
Quick Links
-
How These Shortcuts Can Help
-
Searching Your Command History
-
Quick Command Line Edits
-
Cut and Paste on the Command Line
-
Powering Up cd
-
Tab Completion
-
Some Other Useful Tricks
-
Faster Is Better
Key Takeaways
- Use keyboard shortcuts for quick edits, movements, and searches to enhance command line efficiency.
- Power up the cd command with shortcuts like cd ~ and cd – to navigate your file system.
- Employ tab completion to reduce typing errors and boost speed when working with filenames and directories.
Typing commands in a Linux terminal window can be slow and error-prone. Use these keyboard shortcuts and other command-line tricks to improve your efficiency and accuracy, and avoid frustrations and mistakes.
How These Shortcuts Can Help
The Linux command line gives you a tremendous amount of power. You get unparalleled access to the inner workings of your Linux installation. All the Linux commands, configuration files, and other tools are at your disposal. But the defining characteristic of the command line is also its biggest drawback: commands must be typed.
Typing is relatively slow. It’s also easy to make typos, especially when you’re typing unusual words such as cifsiostat. There’s a danger attached to mistyping things on computers, and Linux is no different.
But, even if your typo didn’t cause a loss of data or some other dramatic outcome, the repetition and annoyance brought about by controlling your computer through the medium of the typed word can sap your enthusiasm for what you’re trying to do.
These hints, tips, and tricks will improve your accuracy, and give a speed boost to your workflow.
Searching Your Command History
Most people know that to repeat a command you can use the command history feature of your shell. All the commands you’ve issued are stored in a list. The history command shows you the list. To replay a command, type an exclamation point ‘!’ followed immediately by the command number, then press Enter.
We’ll show the history list, then replay command number six.
history
!6
You can also use the UpArrow and DownArrow keys to cycle through the commands in the history list. When you see the command you want to run, hit Enter.
With a very long history list, it’s faster and easier to use Ctrl+R to search through your command history.
Press Ctrl+R to start the search, then type a few characters of the command you wish to repeat. The most recently executed matching command is displayed.
In this example, I hit the Ctrl+R combination then typed “ls “, that is “ls” followed by a space.
ls
The characters you type are displayed before the colon “:” so you can see your search clue. You can continue to type characters to refine your search, or press Enter to execute the displayed command.
Esc backs out without executing the command. This is a good way to get the command on the command line so you can edit it.
Quick Command Line Edits
You probably already know the Home, End, LeftArrow, and RightArrow keys move the cursor through the text on the command line. Ctrl+Left Arrow and Ctrl+Right Arrow hops the cursor from word to word.
The Backspace and Delete keys erase text one character at a time, but there are faster ways to remove chunks of the command line you don’t need.
- Ctrl+U deletes from the cursor position to the start of the line.
- Ctrl+K deletes from the cursor position to the end of the line.
- Home, Ctrl+U or Ctrl+U, Ctrl+K deletes the entire line.
- Ctrl+W deletes the word to the left of the cursor.
- Alt+D deletes the word to the right of the cursor.
- Ctrl+Y pastes a deleted word at the position of the cursor. The temporary storage for words deleted by Ctrl+W and Alt+D doesn’t affect your system clipboard.
Cut and Paste on the Command Line
If you want to interact with the system clipboard on the command line, you can do so using keyboard shortcuts. They’re similar to the well-known Ctrl+C, Ctrl+V shortcuts, but you need to use shift at the same time.
- Ctrl+Shift+C copies highlighted text from the command line to the system clipboard.
- Ctrl+Shift+V pastes text from system clipboard to the command line.
Powering Up cd
Using the command line means moving around in your file system. The cd command moves you to another directory. There are also handy shortcuts that will take you back to your home directory.
cd ~, and cd on its own, jump back to your home directory from wherever you are in the file system.
That’s neat enough, but I find cd – more useful. cd, with the ‘-’ option, takes you back to your previous directory. You can use it repeatedly to hop back and forth between two directories.
cd ~/Downloads/taf
cd #
cd -
cd -
The name of the directory you’re returning to is printed in the terminal window.
Tab Completion
You get a serious speed and accuracy boost when you use Tab completion. It removes the possibility of typing errors altogether when you’re working with filenames, directory paths, and other system values.
The technique is the same in all cases. You type a small part of the directory path, then hit Tab. The shell tries to complete the rest of the filename, directory path, or command.
If there are several possible matches, you’ll hear a bleep. Press Tab again and you’ll see the possible matches. You have to type enough characters to get past the point of ambiguity.
Here’s an example. I typed “cd Do” and hit Tab. I heard a beep, so I pressed Tab again.
Bash lists the matching directories, then restores my half-completed command line. In this case, to differentiate between the two directory names, I need only type the next letter. An “n” or a “w” will positively identify the directory I mean.
I typed “w” because I’m going to go to the Downloads directory.
When I press Tab, the rest of the directory name is added for me.
If I wanted to go to a subdirectory, I could start typing its name and hit Tab again. You can keep doing that to quickly get to deeply nested subdirectories.
You can use this technique to complete shell commands, application names, and even your own aliases and shell functions. This is a fast way to add directory path names to commands as parameters, too.
Some Other Useful Tricks
These last few are pretty well-known but, just in case they’re new to someone, these are quite possibly my most commonly used keyboard shortcuts.
- Ctrl+L clears the terminal window.
- Ctrl+L closes the terminal window.
- Super+Left Arrow Snaps the terminal window to the left.
- Super+Right Arrow Snaps the terminal window to the right.
Faster Is Better
The command line is a massively powerful resource, yet it can leave you feeling limited because of its one-letter-at-a-time interface. Tab completion and accelerators like cd – can give you a speed boost without the loss of accuracy you get by trying to type too fast.
Faster is better, especially when it is easier.