Sure, learning the Linux commands should always be your priority but once you get a grip on the command line, there's one other thing you should focus on. Terminal shortcuts! You have no idea how helpful they are until you know how to use them to make your terminal sessions super productive. So in this tutorial, I will walk you through the top terminal shortcuts with examples of how to use them. Before I explain all the shortcuts individually, here's a cheat sheet of what I'll be discussing in this tutorial:
Shortcut | Description |
---|---|
Ctrl + A | Move to the start of the line. |
Ctrl + E | Move to the end of the line. |
Ctrl + U | Delete from the cursor to the start of the line. |
Ctrl + K | Delete from the cursor to the end of the line. |
Ctrl + W | Delete the word before the cursor. |
Ctrl + L | Clear the terminal screen. |
Ctrl + C | Stop the current process/command. |
Ctrl + D | Log out or exit the terminal. |
Ctrl + Z | Pause the current process (can be resumed). |
Ctrl + R | Search command history (backward search). |
Up Arrow | Show the previous command (from the command history). |
Down Arrow | Show the next command (from the command history). |
!! | Repeat the last command. |
!n | Repeat the nth command from history. |
Tab | Auto-complete commands, files, or directories. |
Tab twice | List all possible completions. |
Ctrl + Shift + C | Copy the selected text or command. |
Ctrl + Shift + V | Paste copied text or command. |
Ctrl + Shift + N | Open a new terminal window. |
Ctrl + Shift + T | Open a new tab in the terminal. |
Ctrl + Tab or Ctrl + PageDown | Switch between terminal tabs. |
Though I have used the capital letters, you don't have to type them in caps. I mean Ctrl+A means pressing Ctrl and the A key together. You don't have to press Shift+a key to get the uppercase A.
Now, let's have a look at them individually.When you press the Ctrl + A , it will shift the cursor to the beginning of the file which can be really helpful when you write a long command and want to make changes at the beginning of the line. For example. here, I've demonstrated how you can press the Ctrl + A anywhere and it will shift you to the beginning of the line:
While using the terminal if you want to jump to the end of the line, you can simply press the Ctrl + E and it will do the job. In the following example, I used a sample text and pressed Ctrl + E to get to the end of the line:
There are times when you want to remove everything from the cursor position to the beginning of the line. In that case, all you have to do is use the left arrow keys to place the cursor from where you would like to delete to the start of the line and then press Ctrl + U : Actually, it 'cuts' the text which can be pasted with Ctrl+Y.
Entering the account password with sudo command and unsure whether you typed it correctly? Instead of pressing backspace all the way, just use Ctrl+U keyboard shortcut and start typing the password again.
As you can guess from the title, when you press the Ctrl + K , it will remove (it also 'cuts' the next) everything from the cursor to the end of the line (everything from the cursor position to the right-hand side). To use this shortcut, first, you have to place your cursor from where you want to remove text to the end and then press the Ctrl + K as shown here:
This is what I use daily as I often mistype commands and want to remove one part of the command for that, you can simply press the Ctrl + W . When you press the Ctrl + W key, it will only remove (cuts actually) a single word before the cursor:
It does not clear the terminal screen in a true manner but declutters the screen and if you scroll up, you will still find the previous command and execution history. Yes, it is different than the clear command as it removes the history and you will find the execution of the clear command in the command history. But when you press Ctrl + L , it just declutters your current screen and you won't find it inside of the history (as it is not a command itself). For example, here, I executed the history command and then pressed the Ctrl + L key to clear the screen:
How many times did it happen when you wanted to stop the command execution and you had no idea how to do it and ended up closing the terminal itself? Well, in any case, all you have to do is press Ctrl + C . When you press the keys, it sends the SIGINT signal that will eventually kill the process. For example, here, I killed the ongoing point command execution: In the end, you'll see the ^C symbol indicating you pressed the Ctrl + C to kill the ongoing execution. But there are several processes that may not be killed using the Ctrl + C signal and in that case, you can use the other termination signals in Linux:
You can always use the exit command to close a shell session and terminal. You can also use the Ctrl+D shortcut keys as well. When you press the Ctrl + D , it will log you out from the ongoing session if you use it in SSH, it will close the session and if pressed again, it will close the terminal itself:
Killing an ongoing process is not a good idea always as you have to start over again. So in that case, what you can do is press Ctrl + Z to stop the ongoing process and later on can be continued from where it was left. For example, here, I stopped the update process: Want to know more ways to stop ongoing processes and how to resume them? Here's a detailed guide for that purpose:
When you press Ctrl + R , it opens a search mode prompt from where you can type any part of the command and it will find you the command with matching string you've entered. Once you find that command, you simply press the Enter key and it will execute that command. For example, here, I searched for the update and it gave me the command to update the repository in Ubuntu (sudo apt update):
If you don't like any of the suggestions from the history, use Ctrl+C to come out of the search mode.
When you press the Up Arrow key, it will show you previously executed commands one by one from the command history:
When you press the Up Arrow key, it shows you previous commands but there are times when you accidentally press it many times and now you want to show the previously shown command. In that case, you can use the Down Arrow key. In the following illustration, first I pressed the up arrow key multiple times, and then to come back to previously shown commands, I pressed the down arrow key:
Page Up and Page Down keys are also use for the same purpose.There are times when you want to execute the most recent command one or more times and in that case, you can simply type !! (exclamation twice):
For example, here, I executed an echo command and then used the !! twice to use the same command again and again: But the most common and useful execution of this shortcut is when you forget to use sudo with a command. Instead of writing the entire command again, you just use sudo !!
A similar keyboard shortcut is Alt+.. This one gives you the last argument/part of the last command. Say, you used ls /etc/apt/sources.list.d previously and now you want to enter this directory. Just type cd and use Alt+.. It will be like typing cd /etc/apt/sources.list.d
You can access the history of executed commands by simply executing the bash history command in the terminal and each will have an index number associated with it:
history
Now, let's suppose I want to execute the 2nd last echo command, then I will be using the following:
!1998
I think I should have started the terminal shortcuts list with this one. While typing a long command, you can type half of it and then press the Tab key and it will auto-complete for you. For example, here, I pressed the Tab key to auto-complete my script execution:
If pressing the Tab key does not work, it means there are multiple possibilities of the currently typed command. In that case, what you can do is press the Tab key twice to list all the possibilities:
The next few shortcuts are terminal emulator dependent. They should work with most terminal applications but that's not a surety.
To copy the text in the terminal, you have to select the text using the mouse and then press the Ctrl + Shift + C to copy the selected text:
Once you copy the text by selecting the text and pressing Ctrl + Shift + C , now you can paste it anywhere by pressing Ctrl + Shift + V :
When you press the Ctrl + Shift + N , it will open a new terminal window with the same working directory you were working in the previous window:
If you created multiple tabs using the above method, you may want to switch between them. And for that purpose, you can use Ctrl + Tab or Ctrl + PageDown :