Navigating the terminal is easier if you know some useful shortcuts. The goal is to not use the arrow keys on your keyboard to navigate around the commands you have typed. Also, you don’t need to type everything again as there are shortcuts like searching the history or reusing a command or even its arguments.
In this blog, you will learn some of the useful Linux and macOS terminal shortcuts.
Move the cursor to the beginning of the line
Sometimes you type a pretty long command and then realize you need to return to the beginning of the line to either add options or make modifications to the command itself. Using arrow keys to bring your cursor to the beginning of the line is not efficient and you should never use arrow keys. It’s especially annoying if the command you typed is pretty long.
You can use
ctrl + a to bring your cursor to the beginning of the line.
Move the cursor to the end of the line
Other times, you may need to move the cursor back to the end of the line. For example, after you added a forgotten option to your command, you want to continue with it and would need to move the cursor back to the end of the line. You can do this by
ctrl + e.
I personally use these two shortcuts quite a lot as they’re very handy and can save a lot of time.
Delete everything before the cursor
You can delete a whole line or parts of it by using
ctrl + u. This shortcut removes everything to the left of the cursor and it comes handy when you want to delete everything and start over.
For example, let’s say we have typed
ls -l /etc/hosts, now I want to remove the
ls -l part and perhaps use another command but keep the path. To do that, place the cursor right after
-l option and press
ctrl + u and that will remove the
ls -l part and you’re left with
/etc/hosts. Of course, this is a simple example but this can be useful when the command is pretty long like when it contains a long file path.
Delete everything after the cursor
You can delete everything to the right of the cursor with
ctrl + k. This is exactly the opposite of
Ctrl+ u. It removes everything that comes after the cursor.
Delete a word before the cursor
Another very useful shortcut is when you want to delete the word right before the cursor. For example, if you have type cat
/etc/hosts and your cursor is placed at the end of the line, you can get rid of the word before the cursor, i.e /etc/hosts, by pressing
ctrl + w.
With that, you don’t need to keep pressing the backspace key.
Clear the screen output
I have been guilty of typing
clear command when I want to clear the terminal output. It’s probably a habit more than anything as I also know the shortcut. I’m trying to get used to using
ctrl + L to clear the terminal output.
One big advantage of using the
ctrl + L over the
clear command is that with
ctrl + L you can clear the terminal even after you’ve already typed your command. it will clear the previous command output and still keep the command that you’re just about to execute.
Search the history
You can search your command’s history from the terminal. I can’t emphasize the importance of this shortcut enough. It’s one of those shortcuts that you must know as it saves you a lot of time.
If you don’t remember a command that you had previously run that you want to run it again, use
ctrl + r.
ctrl + r, a new prompt will open to you and you can start typing parts of the commands that you’re interested in. As you type a few characters, the terminal will give you auto-suggestions based on what you’ve typed so far. You can press Enter to execute the command if the suggestion is what you’re looking for.
If the suggestion is not what you’re looking for, for example, you’re looking for another instance of the same command, you can press
ctrl + r again and again and it will loop through all the instances of the command based on your typed query.
This is quite handy as you will forget a lot of things on your terminal and you definitely need this to find your history.
Re-execute a command from the history
You can execute any command from your command history by typing
n is the number assigned to the command. For example, if you want to execute the 3rd command from your history, you can type
!3 and press Enter.
This is very useful with the use of the
history command along with
grep searching for a command that you have executed previously. For example:
history | grep 'openssl'
This will return my previous
openssl commands. It will also display the number in front of the command and you can use
!n to re-execute it.
In this example, the
openssl command that I want to run is numbered 59, therefore, I will run:
That will execute the command:
openssl x509 -in server.crt -noout -text
When you run a command in the terminal, Linux saves it in a file called
.bash_history. This file is typically located in your home directory.
The history command that we discussed earlier simply displays the contents of the
.bash_history file. Each command in the history is numbered, starting from 1, and you can use these numbers to re-execute a command using the
!n syntax as we discussed earlier.
Knowing where the history file is stored can be useful if you want to back up or transfer your command history to another system.
Access arguments of the previous command
Sometimes it’s handy to reuse the arguments that you had passed to the last command you ran. For example, let’s say I ran the following command:
Now I want to use the
/etc/hosts argument with a new command, let’s say
vi. Instead of using
vi /etc/hosts, I can use:
!$ here represents the last argument from the previous command (/etc/hosts).
If you want to access all the arguments from the previous command, you can use
! together with
For example, if the following is my previous command:
ls /etc/hosts /etc/passwd
I can access both arguments (/etc/hosts and /etc/passwd) by using
Logout or exit from the terminal
It’s easier to use
ctrl + d when you want to exit from a user of terminate your SSH connection for the server. You can use
exit commands, but the shortcut is
ctrl + d and it’s one of those shortcuts that you will use more often that you think.
In conclusion, you short use as few key strokes as possible when navigating the terminal. The arrow keys and backspace key usage can be significantly minimized if you know these shortcuts.