As a programmer, we tend to use the terminal a lot and use many commands daily repetitively. In today's post, we are going to cover a few tips and tricks to reduce your keystrokes and increase your productivity.
a) Creating Aliases
We tend to ssh
into different servers, run local servers and a hell lot of other stuff daily, before beginning our actual coding. Creating aliases for longer commands helps us run our daily commands with lesser keystrokes.
So here are the steps:
1) Open the terminal and type vim ~/.bashrc for Linux, or ~/.bash_profile in macOS.
2) Check if there is the following line:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
3) If present, then quit .bashrc and open .bash_aliases file using gedit
, or some other text editor.
sudo gedit ~/.bash_aliases
If it's not present, then continue editing the .bashrc / .bash_profile file itself.
4) Enter aliases with the following syntax:
alias 91='ssh root@10.130.98.91'
alias http= 'http-server -p 9001'
5) Save the file using :wq
in vim, or hitting save in gedit.
6) Run source ~/.bashrc
.
7) Next time you type 91 in the terminal, it will automatically ssh
into the required server.
b) Using ! to run the last command:
If you ran a command like http-server -p 9001
, then typing in !http will run that command directly.
You can run the last command by simply typing !!... which is very useful for those cases when you forget to type sudo
before a command. You can also use !$ to use the last argument.
saurabh@HOMEPC:~$ mkdir /etc/opt/mydir
mkdir: cannot create directory ‘/etc/opt/mydir’: Permission denied
saurabh@HOMEPC:~$ sudo !!
sudo mkdir /etc/opt/mydir
saurabh@HOMEPC:~$ sudo rm -rf !$
sudo rm -rf /etc/opt/mydir
c) Using crtl+r
to search for commands:
1) Type crtl+r
to start reverse-i-search. Type in a keyword which you remember using in the command, and you will get the search result.
2) Press enter to run the command.
d) Using pgrep to find pid:
In order to kill/restart a process we usually need pid of the process. Instead of using the crazy combination of ps and grep simply use pgrep.
saurabh@HOMEPC:~$ pgrep firefox
3192
saurabh@HOMEPC:~$ kill 3192
e) Using a combination of history and grep
This allows you to retrieve the commands which you have used a long time back.
saurabh@HOMEPC:~$ history | grep "http"
750 http-server -p 8000
765 http-server -p 8000
887 http-server -p 8000
896 http-server -p 8000
919 http-server -p 9000
926 http-server -p=9000
f) Check all of the ports used by all of the processes, in the system:
netstat -tlnp
Here are some additional commands and utilities which you may use daily for some general purposes:
Unzipping archive in terminal:
Many times new software/packages are downloaded with the extension of .tar.gz In order to extract it in terminal type:
tar xvzf download.tar.gz
The breakdown of xvzf
is as follows:
x is used to specify that we are extracting the archive (c is for compressing)
v is to specify that verbose mode is enabled (gives output while the action is being performed)
z is to specify that file we are working on, is gzipped
f is to specify that we are passing a file
Install and use htop
rather than top
:
Htop
adds some extra good looks and utilities to the top
command. For system administrators, this tool might come in handy.sudo apt-get install htop
Using diff and patch commands for faster patch additions
Instead of using an online diff checker we can check differences between two files directly from the terminal using the diff command. Patch command can be used to patch the changes to an old file.
saurabh@HOMEPC:~/Documents$ diff nf.txt of.txt
1a2
> 234
saurabh@HOMEPC:~/Documents$ diff oldFile newFile > changes.patch
saurabh@HOMEPC:~/Documents$ patch oldFle changes.patch
Use terminator/iterm/tmux rather than opening multiple terminals
It is high time that you switch to terminal emulators like terminator (Linux), iTerm(macOS) or tmux, so that you can open multiple terminals in a single window rather than opening multiple terminal windows.
You can also use the broadcast feature in these emulators to run a single command in multiple terminals simultaneously.
You can even save a custom layout as default in terminator by which it will open with same layout each time you open it.
The link to add custom layout can be found here
Shutdown from terminal:
Last but not the least, as many of you might know already, use the following command to shutdown from terminal:
sudo shutdown -h now
where:
-h is for halt
-r to reboot
or you can alternatively use:sudo poweroff
or
sudo reboot
P.S. It's good to make an alias for shutdown for using it everyday
Display matrix code in terminal
This is just a funny command to show off in front of your friends.
Many of you who have seen the movie Matrix might like to see a similar kind of animation in your terminal.
A package called cmatrix can get your job done quite easily.
To install cmatrix run the following command:
sudo apt-get install cmatrix
Run cmatrix from your terminal and see the following output:
That's it for today's story. Go try some of these commands. Happy coding!
Kindly upvote the article if you like it and mention some more terminal tips and commands in the comments section if you would like to.