Navigating the terminal efficiently can greatly enhance your productivity. By utilizing a variety of useful shortcuts, you can avoid the need for arrow keys, streamline command execution, and reduce repetitive typing. This guide will introduce you to some essential Linux and macOS terminal shortcuts
Moving the Cursor
Move to the Beginning of the Line: When you need to quickly return to the beginning of a long command to add options or make modifications, avoid using arrow keys. Instead, use:
Ctrl + A
Move to the End of the Line: If you need to move the cursor back to the end of the line after making changes, use:
Ctrl + E
Deleting Text
Delete Everything Before the Cursor: To delete all text to the left of the cursor, use:
Ctrl + U
For example, if you typed ls -l /etc/hosts
and want to remove ls -l
, place the cursor after -l
and press Ctrl + U
.
Delete Everything After the Cursor: To delete all text to the right of the cursor, use:
Ctrl + K
Delete a Word Before the Cursor:
To delete the word immediately before the cursor, use:
Ctrl + W
For instance, if you typed cat /etc/hosts
and want to remove /etc/hosts
, press Ctrl + W
.
Clearing the Screen
Clearing the terminal screen can be done with:
Ctrl + L
This command clears the screen but retains the current command you're typing, unlike the clear
command.
Searching and Re-executing Commands
Search Command History: To search through your command history, use:
Ctrl + R
Begin typing part of the command, and the terminal will suggest matches. Press Enter
to execute the selected command, or Ctrl + R
to cycle through more matches.
Re-execute a Command from History:
To execute a specific command from your history, use:
!n
Where n
is the command's number in your history. For example, !3
will execute the third command in your history.
To find a specific command and its history number, you can combine history
with grep
:
history | grep 'openssl'
Then, use !n
with the corresponding number to execute it. For example, in the following case, the openssl command that I want to re-execute is 59.
Reusing Arguments
Reuse Last Argument: To reuse the last argument from the previous command, use:
!$
For example, if your last command was cat /etc/hosts
, typing vi !$
will be equivalent to vi /etc/hosts
.
Reuse All Arguments:
To reuse all arguments from the previous command, use:
!*
If the previous command was ls /etc/hosts /etc/passwd
, using cat !*
will be equivalent to cat /etc/hosts /etc/passwd
.
Exiting the Terminal
To log out or terminate an SSH connection, you can use:
Ctrl + D
This shortcut is quicker than typing logout
or exit
.
Conclusion
Mastering these terminal shortcuts can significantly improve your efficiency by reducing the number of keystrokes and avoiding repetitive actions. By integrating these shortcuts into your routine, you can navigate the terminal more effectively and save valuable time.