Now a days there are a lot of modern IDEs available and most of the developers are using these, like Sublime Text, Eclipse, Visual Code etc. But the limitation to these IDEs are that one can’t use these in terminal, even though these are great IDEs to work with.
The reason why we want to use these IDE in terminal because sometime we may need to perform some action in remote servers. Than comes the world of terminal based editors, such as vim, emacs, nano etc. Now it depends on how you have mastered in any of these editors, which makes you feel comfortable while doing anything remotly on fly.
In this article we are going to discuss about how you can boost productivity in vim. Vim is available for many different systems and there are several versions. If you are using Ubuntu,
apt-get install vim should install the vim in your system. If you are using any other operating system, please check here to know more about it. Now lets discuss how we can improve productivity in vim.
Turn On Relative Line Numbers
To delete some lines of text when you don’t know exact number of lines to delete, you try
3dd until you remove all the lines. Relative line numbers solves this problem, suppose we have a file containing :
sometimes, you see a block of text. You want to remove it but you cannot directly get the exact number of lines to delete so you try 10d , 5d 3d until you remove all the block.
Enter NORMAL mode:
Esc, now execute
:set relativenumber. Once done the file will be displayed as:
3 sometimes, you see a block of 2 text. You want to remove 1 it but you 0 cannot directly get the 1 exact number of 2 lines to delete 3 so you try 4 10d , 5d 5 3d until 6 you 7 remove all the block.
0 is the line number for the current line and it also shows the real line number in front of relative number, so now you don’t have to estimate the numbers of lines from the current line to cut or delete or worse count them one by one.
You can now execute your usual command like
6dd and you are sure about the number of lines. You can also use the short form of the same command
:set rnu to turn on relative numbers and
:set nornu to turn off the same.
If you also
:set number or have
:set number already on, you’ll get the line number of the line in which the cursor is at.
3 sometimes, you see a block of 2 text. You want to remove 1 it but you 4 cannot directly get the 1 exact number of 2 lines to delete 3 so you try 4 10d , 5d 5 3d until 6 you 7 remove all the block.
Using the path completion feature inside Vim
This is very common, you memorize a path to a file or folder, you open up Vim and try to write what you’ve just memorized, but you are not 100% sure it’s correct, so you close the editor and start over.
When you want the path completion feature, and you have a file
/home/ubuntu/my_folder/my_file and you are editing another file referencing to the path of the previous one:
Enter insert mode:
insert or do it the way you want. Next, write
/h. When the cursor is under
Ctrl+x and then
Ctrl+f so the editor will complete it to
/home/ because the pattern
/h is unique
Now, suppose you have two folders inside
my_folder_2 and you want the path
/home/ubuntu/my_folder_2, as usual:
- Enter insert mode
- Now you have
/home/, next add
- Now you have
/home/ubuntu/because that path is unique.
- Now, write
Ctrl+f. The editor will complete your word until
my_folder_and you will see the directory tree so use the arrow keys to choose the one you want.
Write a file if you forget to
sudo before starting vim
This command will save the open file with sudo rights
:w !sudo tee % >/dev/null
You can also map
w!! to write out a file as root
:cnoremap w!! w !sudo tee % >/dev/null
Know the path of the file you are editing
To know the path of the directory your file is in you can use
Esc to enter command mode, and then type
:pwd. This will print the path to the directory at the bottom of the editor.
Revert the document back to how it was few minutes ago
To revert the document back to how it was 15 minutes ago, you can hit
:earlier 15m. It can take various arguments for the amount of time you want to roll back, and is dependent on undo levels. Can be reversed with the opposite command
Open source of any URL
vim can be used to see source of any webpage, to see source of any website hit:
$ vim 'https://codingdash.com/'
Do you know any hack or quick way to do anything other than these? Please share in comments. Happy hacking!!