Vim is one of the best and commonly used text editor and could be used as IDE on Linux and MAC OS X. There are many Vim tips could help you to get your work done much more quicker and efficient if you are using Vim as your text editor. So, let’s check some of Vim Tips that could be helpful for your daily usage.
1. Search for a Pattern in Vim
This is one of the most common usage, we would do if we are looking for a pattern inside a text file. To search we need to be in the normal mode of Vim and type “/” and followed by the pattern we are looking for.
To jump to the next search result use “n” and to get back to the previous search result use “N” uppercase “Shift+n”.
Let’s say we have a word with starting with a capital character and we want to search for all words whether it has UPPER case or lower case characters. we will need to set case insensitive options with the following command. Enter command mode “:”
and re initiate the search using same method mentioned earlier and it will now ignore character case and we will get all word matches the pattern.
Also, you could open Vim directly from the shell to the required search pattern using the following command.
Vim text-file.txt -c "/hot"
2. Search and Replace in Vim
The simplest method to search and replace a word in the entire text file or even a single line.
In the following example we will replace word “hot” with “warm” word in the entire text file, we need to include “%” sign to the command to replace the word in the entire file.
Another example to search and replace in the same line you are currently selecting.
To ignore case insensitive to replace a word, include “i” as shown the following command.
3. Undo and Redo in Vim
Incase you have miss-edited text file and your want to toggle between the recent changes in Vim. you could use the following options in the Normal Mode of Vim.
To Undo in Vim press “u”.
To Redo in Vim press “hold Ctrl button + r”.
4. Delete a Line in Vim
In the Normal Mode of Vim. Select the line and press d twice “dd”.
5. Jump to the End of the Text File.
To jump the last line in the text file. “Hold Shift + g” and Press “End” button to jump to the end of the line.
6. Jump to the TOP of the Text File.
To jump to the first line in the text file. Press double g “gg”.
To jump to the first line visible in the screen not the first line in the text file. “Hold Shift + h”.
To jump to specific line. Enter the Command Mode of Vim.
It will take you to line number 20.
Or, your could directly access Vim the required line from the shell using the following command.
Vim text-file.txt -c ":20"
7. COPY, CUT and PASTE in Vim
First we need to enter “Visual” mode by pressing “v” and you could move up, down, right and left with keyboard arrow keys to select the required text you want to cut or copy.
After you are done with selecting the text. Press “y” which represent yank in Vim language to copy the text.
Select the location you want to paste the text and press “p” in Normal mode of Vim.
CUT is quite similar, we will use the same procedure mentioned earlier except we will replace yank with “d” which delete or cut the selected text and your can paste it with “p”.
8. Display the number of lines in Vim
You could display the number lines inside Vim using the following command in the Command Mode of Vim.
To turn numbers off, use the following command in the Command Mode of Vim.
9. Comment a portion of code or configuration files in Vim
It depends on what are you trying to comment some types of code or configuration files could be commented with semicolon “;” or hash “#”. You could comment any with the same procedures.
Enter Visual Block mode by “Hold Ctrl + v” and selecting the beginning of the lines your want to comment using up and down arrow keys.
Afterward, Press ” Shift + i “ to enter insert mode to the first line you have selected and add the comment character, in my case it would be hash “#”. Finally Press “Esc” key and it will add the comment the rest of the lines for you.
Also, you could re-select with visual block again the commented lines to uncomment them by pressing “d” to delete the comment of the beginning of the line.
10. Remove duplicated identical lines and sort in Vim
A quick method to delete duplicated lines and sort it by it’s numerical value. Enter Command Mode in Vim and type the following command.
Let’s say we want to sort from line 20 to line 100. Use that following command in that case.
Vim text editor is quite powerful if you have compared it to any text editor runs through the command line interface and it comes with many features and functionality would be available on other text editors through a graphical user interface only. It even include features still doesn’t exist in other text editor with GUI support. The more you use Vim with different scenarios, the better tips and tricks you will discover in Vim Text Editor.