Useful Linux Terminal shortcuts

Here are some useful Linux Terminal Shortcuts:

Shortcut 1: Run previous typed command

In your Linux terminal, type:

# ls -l

Now, if you want to run this command again i.e the (previous command), type the following and hit ENTER:

# !!

This will run the command which was previously typed i.e ‘ls -l’.

Shortcut 2: Run command with last argument from previous command

For this example, lets create an empty file one.txt:

# touch one.txt

Now, lets use the following command:

# cp one.txt one.bak

Lets break down this command according to its arguments:

1st argument – one.txt

2nd argument / last argument – one.bak

[ Note: We can logically say that the 0th argument is the command cp itself. ]

Lets say we want to edit the file “one.bak” (which is the last argument for command cp, we can use:

# vi !$

The above command is same as running ‘vi one.back‘. Also note that the !$ passes the last argument of the previous command to the current command vi.

Shortcut 2 (Alternative):

Now, lets use the following command:

# cp one.txt one.bak

An alternative way to provide an argument from the previous command to another command is by using !:<argument number>. For example, to pass the 2nd argument of the last command, we use !:2 as follows:

# vi !:2

Here !:2 means that we are passing the 2nd argument of the previous command.

Shortcut 3 : Linux Terminal Keyboard shortcuts

To get the previous command: Press the UP arrow in the keyboard.

To kill a process when you are in the Linux terminal: Press <CTRL> + C in your keyboard.

Shortcut 4 : Autocomplete:

Trust me when I say this, this shortcut makes your life easier. If you need to auto complete a command, you can use the TAB key in your keyboard. This is one the shortcuts that is used by most Linux users.

Example:

If you want to type ifconfig in your Linux terminal, you can start typing the command and then press the TAB key to auto complete it. In this case, type ifcf and then press TAB and it will autocomplete the command.

The TAB feature can also be used to autocomplete known information such as file names which are passed as arguments.

For example: Lets assume in the current directory that you are on has only two files first.txt and second.txt. Now, if you need to type this command ‘cat first.txt second.txt‘, then you can use the TAB shortcut to auto-complete it by following these steps:

Type cat and then type f and then press TAB to autocomplete it to:

 cat first.txt

Then type the first letter of the next argument i.e. type s and then press TAB, which it autocomplete the command to:

cat first.txt second.txt

Shortcut 5: Move between words

  • Navigate to the next word from current position:

Press <CTRL> and <Right Arrow key> in your keyword to move to the next word(towards the right side of current cursor position) in your terminal. This is also applicable in Linux editors such as vi or vim.

  • Move to previous word in terminal from current position:

Press <CTRL> and <Left Arrow key> in your keyword to move to the previous word/the word to the left side from current cursor position in your Linux terminal (or most editors). You can press it again accordingly, if you need to move the cursor one word at a time towards the left direction.

The above two shortcuts are useful when you are entering commands in the Linux terminal and need to correct a particular word in the command. This helps navigating back and forth between words/parameters in the command with ease.

Other Bash Shortcuts:

To go to the 1st terminal, press CTRL, ALT and the function key F1 in your keyboard.

To go the nth terminal, press CTRL, ALT and Fn (where Fn can be function keys F1, F2, F3 etc).

To paste previously copied text to your Linux terminal/editor, you can press the middle button in your mouse. Now, How cool is that uh? 🙂

To lock your Linux machine press <CTRL> <ALT> and the letter l in your keyboard.

And that’s it for now, hope its been informative. I will be updating this article in the future with more Linux terminal shortcuts  as soon as I find new ones, so stay tuned! If this article helped you, do leave a comment below and like us on Twitter and Facebook. Cheers!

Hope this helps!