Terminal Tricks and Shortcuts - Hidden Gems!
Developer Productivity - Part 1
As a developer, most of our time is spent on the terminal. We get things done with our known set of commands mostly.
But there are so many hidden gems inside our terminal that will elevate our skills and productivity.
In this series part 1, I'm gonna share a few things that will work for all the commands.
Most of the times we work inside the terminal in following patterns
- Same Command with different arguments
- Different command with the same arguments
In this case, we either retype the whole thing or edit the previous command/input. With the following set of tricks, we can do better.
! is the operator which is going to help us in these scenarios
⇄ Tab Or ↵ enter can be used to replace these substitutions
Same Command with different arguments
!! Operator - Substitues Last Command
Many times we would have run to a scenario where admin rights are needed for command.
>cat system.log
You need administrator access
and then we add sudo
and type the command again. But there is a better way, type sudo !!
and enter ↵
or ⇄ Tab
> sudo !!
sudo cat system.log
<log file content>
We can also add alias su='su !!'
to our profile
!:0 - Previous Command without arguments
To repeat the previous command with a different argument then type !:0
and press ↵ enter that will insert the previous command without the arguments and modifiers
> cat abc.txt
> !:0 (↵ enter)
> cat |
To run it with some parts then !:0-n
will get till the nth part of it
> ssh -i dev-uswest.pem ubuntu@10.801.118.256
0 1 2 3
> !:0-2 (↵ enter)
> ssh -i dev-uswest.pem |
!prefix - Repeat previous command that starts with a specific word/letter
To repeat the last command which starts with ssh
then
>!ssh (↵ enter)
>ssh -i uswest.pem ubuntu@10.01.118.256
!?str? - Last command containing str
To repeat the command which contains sedhu
from the history
> !?sedhu?
> ssh -i uswest.pem sedhu@10.801.118.256
Ctrl + R Reverse Search
Search through the commands history and Keep Pressing Ctrl + R
and Ctrl+Shift+R
to continue searching Forward and Backward.
~/.aws ⌚ 21:31:24
$ ssh -i uswest.pem ubuntu@100.81.112.257
bck-i-search: ssh_
!n - Nth command from the history
If we remember the command history
position, we use !n
and !-n
to execute the command in position from Top and Bottom of the list.
If the history is as follows
> history
1 git push -f --no-verify
2 npmR test
3 npmR start:dev
4 npmR test
then results of such commands will be
> !1 // git push -f --no-verify
> !-1 // npmR test
> !-2 // npmR start:dev
Different command with the same arguments
!$ Substitutes last argument
Let's say we were viewing a config file with the cat
command
/etc/vim ⌚ 15:12:23
$ cat vimrc
<vimrc file content>
now we need to edit something in that, type vi !$
and ↵ Enter
/etc/vim ⌚ 15:12:31
$ vi !$ (↵ enter)
/etc/vim ⌚ 15:14:50
$ vi vimrc
Esc . (Dot) - Inserts last argument
Suppose we need to inspect before executing that command then type Esc .
which will insert the argument
/etc/vim ⌚ 15:15:32
$ vi (Esc .)
$ vi vimrc
Few more
!^
- Substitutes first argument from the previous command!*
- Sustitues all arguments from the previous command!n
- Nth Argument from the previous command!-n
- Nth Argument from the previous command (Backwards)^val1^val2
- Last command replacing val1 for val2
> ssh -i uswest.pem ubuntu@10.81.118.219
> ^ubuntu^sedhu (↵ enter)
> ssh -i uswest.pem sedhu@10.81.118.219