Quick tricks that will save you time
Table of Contents
Working with the command line can be fun. But the more you use the command line, the more you realise that you end up repeating commands over and over again in your workflow.
Luckily there are some easy to remember commands that will help you cut down time when repeating commands. You might also be interested in checking the command line shortcut cheatsheet
History - Using reverse-i-search
Reverse-i-search will search through the whole history
to match a command that contains what you typed. You will need to activate reverse-i-search by pressing the keys CTRL+R
, your prompt will switch to (reverse-i-search)`':
With reverse-i-search activated, you can type the beginning of a command and the last one that was run will be matched. If you want to cycle through all matches you just need to press CTRL+R
again.
If you know that you are going to use a command a lot you can write the command like this:
<command> #tag
Then all you need is run the reverse-i-search and search for #
, your command will be matched straight away. Since the # indicates a start of a comment the terminal will simply ignore the text #tag
.
Examples
History:
shell11 git pull origin master #pull-origin-master2 2 git status3 3 git stash4 4 git pull origin master5 5 git stash apply6 6 git add file7 7 git commit -m "Added file"8 8 git rebase -i HEAD~29 9 git push
Start reverse-i-search and type git:
shell1(reverse-i-search)`git': git push
Git push was the last command typed so that's the first match that reverse-i-search returns.
Now let's use the tag trick to run the first command in our history. Let's reverse-i-search for #
shell1(reverse-i-search)`#': git pull origin master #pull-origin-master
History - Using !
If you run the command history
you can see a long list with all the commands that you have typed in the command line. To re-run a command you can do one of these two ways:
- Type:
!<number of the command>
- Type
!<first letters of command>
For you to use the number of the command you probably would have to run history
, then get the number and type !<number of the command>
, in some cases this will take even longer than typing the command again.
But since you know which command you want to run, you can simply type !<first letters of command>
and those first letters will be matched to a command. This little trick can save you some precious time.
Examples
History:
shell11 ls2 2 cd3 3 cd documents/4 4 mkdir folder5 5 rmdir folder6 6 ls
Let's run the last command in our history again by typing !!
shell1user ~/documents > !!2ls3folder
As you can see the terminal will print the command before running it, this is to help you in case something happens that you weren't expecting.
Now let's try to run the second command in our history by using the number reference.
shell1user ~/documents > !22cd3user ~ >
The cd
command was run and you should be in your home directory now.
Let's say you want to create a folder with the same name in your home directory you can do:
shell1user ~ > !mk2mkdir folder3user ~ > ls4folder
Autocomplete
Typing long commands can take a while, luckily your terminal can autocomplete commands for you, all you need to do is start typing a few letters and press the TAB
key and the command will be auto-completed.
If you forgot how do you type a command, but you know the first letter, you can type the first letter and press the TAB
key twice, a long list of all available commands will appear and you can use that to help you.
Examples
Let's look for a command that starts with the letter K:
shell1user ~ > K <TAB><TAB>2kadmin.local kextfind keytool klist kswitch3kcc kextload kill kramdown ktutil
As you can see any command that starts with the letter K will be returned.
I hope you find this helpful, let me know if there are more useful commands that I should add to this list!