Using Linux means – sooner or later – using the command line. But typing commands in a terminal window can be slow and repetitive. These keyboard tips will greatly enhance your terminal window experience.
Linux is a command line operating system. You can choose to bring a desktop environment over it, such as the GNOME or KDE desktop environment, but underneath is still a text interface that hasn’t really changed since the first Unix implementations in the 1970s.
In a terminal window, it uses a Linux shell like Bash to read your commands and either act on them itself or pass them to the appropriate command or application.
New shells were released, old shells were almost retired, and some of the long-standing shells were updated. Regardless, what hasn’t changed is the need for users to enter commands. The user must enter their instructions at the command line, just as they did half a century ago.
Commands must be spelled correctly, and since they are case sensitive, they must also be case sensitive. Commands also contain all sorts of symbols and can be extremely long if they involve file system paths. And the longer the command and the more unusual symbols it contains, the greater the risk of errors.
Learning countless Linux commands and harnessing the power of the command line is challenging enough without having to deal with typos and other keyboard errors.
The hints, tips, and techniques we’ll show you will make using the command line faster and more productive.
RELATED: Command Lines: Why Do People Still Bother With Them?
Completing the card
If you type enough letters for the shell to match a command, file name, or directory, pressing “Tab” will insert the matching command, file name, or directory on the command line for you.
In our home directory on our test machine we entered:
Then we press the “Tab” key.
The rest of the “Documents” were added for us. You can keep doing this to materialize the entire journey.
We know that we have a directory called “projects” inside the “Documents” directory, so typing “pro” and pressing “Tab” will complete that as well.
If you can’t remember how the name of the next directory starts, press “Tab” twice and the shell will show you a list of possible directories. It then fills the command line with as many commands as you typed and waits for further input.
Type a few letters of the desired directory – enough to distinguish it from other entries – and press “Tab” to have the shell add it to your command line.
If you do not enter enough characters to uniquely identify the desired directory, a list of directories that match what you have entered so far will be displayed.
Using “Tab” to generate directory paths is an easy way to increase productivity. Completion of tables is also smart, it behaves differently for different commands. If you used
ls instead of
cdit knows it should contain both files and directories.
Pressing “Tab” twice includes files in possible matches.
You can also use “Tab” with command names. Let’s say you want to use
systemctl command to enable the daemon. Type “sudo sys” and press “Tab” twice.
You will see all the commands that start with “sys”.
Add “temc” to your command and press “Tab” once more to complete “systemctl”. Now type “en” and press “Tab”.
sudo systemctl en
The rest of the word “enable” is added to the command line.
The best way to avoid typos is to not write at all. Use “Tab” and let the shell do it for you.
Editing keyboard shortcuts
While tabbing helps, you’ll still have to go through the command you typed and make changes.
There are plenty of keyboard shortcuts to speed up your movements and edits on the command line. Get them locked into your muscle memory and you’ll never have to hold down the arrow keys and wait for the cursor to move back and forth again.
- Ctrl+A: Move to the beginning of the line. The same as Home.
- Ctrl+E: Move to end of line. The same as End.
- Alt+F: Move forward by line one word in time. The same as Ctrl+Right Arrow.
- Alt+B: Move backward one line one word in time. The same as Ctrl+Left Arrow.
- Ctrl+F: Move forward by line One letter in time. The same as Right arrow.
- Ctrl+B: Move backward one line One letter in time. The same as Left arrow.
These shortcuts will remove the text.
- Ctrl+U: Delete from the cursor position to the beginning of the line.
- Ctrl+K: Delete from the cursor position to the end of the line.
- Ctrl+W: Delete a word to the left. The same as Alt+Backspace.
- Alt+D: Deleting a word to the right.
- Ctrl+/: Return. Yes, the command line has an undo option.
A quick “Ctrl+U, Ctrl+K” deletes the entire line.
Why bother rewriting something you’ve already written? Bash stores your previous commands in the command history and allows you to replay any command.
RELATED: How to use the history command in Linux
From the command line, use the Up Arrow and Down Arrow keys to scroll through the list of commands. Press Enter to execute the displayed command. You can use
history to display the entire list of commands.
Commands are numbered.
To replay any of the commands, use the exclamation mark “!” immediately followed by the number of the command you want to use again. Do not put spaces after the exclamation mark.
You can use the beginning of the command itself instead of the command number. For example, if you use
!geditthe shell will execute the last command you used that started with “gedit”.
To be safe, you may want to use
:p (print) modifier. This will print the command but not execute it. Again, do not include any spaces in the command.
If the command found is the one you wanted to run, you can run it by pressing the Up Arrow key to bring it back to the command prompt, then press Enter. Of course, you can press the Up Arrow key and then edit the command before pressing Enter if you need to edit it.
Two exclamation points”
!!” represent the last command. If you forget to use
sudo using the command type “sudo!!” to rerun the last command using
Another useful tidbit is that you can use Alt+. (period) to append the last word of the previous command to the command line.
Interactive history search
Press Ctrl+R to start the search. Then type a few letters of the command you’re looking for and press Ctrl+R.
If a match appears but is not the command you want, press Ctrl+R again to move to the next match. Press Ctrl+R until you see the command you want.
Press Enter to run the command, press Home, End, Right Arrow or Left Arrow to edit the command before it runs.
Ctrl+G exits the search without doing anything.
Other useful shortcuts
These shortcuts are also a must.
- CD: Takes you to your home directory. As
- CD-: Jumps back and forth between the last two directories.
- Ctrl+l”: Clears the terminal window. Same as the clear command, but doesn’t clear your history.
- Ctrl+d: Closes the terminal window. Same as exit command, but does not cover your history.
- Super key + arrows: Snaps and resizes the terminal window to the left and right of the screen, to full size and back to normal size.
How to learn these keyboard shortcuts
Make a shortlist of commands that sound the most useful and write them down on a piece of paper. Keep it close, refer to it, and use these keyboard shortcuts.
Once you have them, start a new shortlist.
RELATED: 37 Important Linux Commands You Should Know