What is VIM? A beginners guide.
In this blog we will learn about most popular linux text editor Vim.
Note: that this article is explicitly for beginners.
Vim is the editor of choice and many power users and developers are using it. It's a "modal" text editor based on the vi editor written by Bill Joy in the 1970s for a version of UNIX.
When we say "modal", its mean that vim has many modes, one of them is common mode or command mode, in this mode, alphanumeric keys (i.e., a through z, and 1 through z) are used as commands unlike other editors where you can use these keys to enter text only or have to use special keys like as CTRL, SHIFT, with these keys to pass a command eg CTRL + S to save changes in LibreOffice but in vim you can do it with ":w" in command mode and this functionality of VIM is called "Modal".
In Vim, you can do many operations without leaving your hands from the keyboard, and sometimes without even leaving the home keys.
If you are one of the following, you probably want to look into Vim:
Heavy editor of plain text files
Work in CLI mode (e.g ssh to remote host).
Some people disagree on how many modes Vim actually has but following 3 modes are most important and you should learn about them.
Last Line Mode.
Are you ready to go next?
Let's install vim if you don't have by typing below command
apt -y install vim
yum install vim
How to start vim?
To start vim we have to type vim filename_with_path and press enter. For now, we assume that you are in your home directory and want to edit a file in /var/log/syslog.1 our command will be as below. If your file is in the same directory where you open your terminal then vim filename will be
When you type above command and hit Enter you will see the content of this file if present and if there is no file with this name vim will print "New File" in the bottom left corner.
Now we will learn about vim modes.
When you open a file in vim, you will be in command mode. This means that all alphanumeric keys are bound to commands and when you press a key it will run a command instead of inserting a character in this file (e.g. pressing "j" will move the cursor down one line).
Most popular commands, you'll want to remember the following keys and what they do:
h moves the cursor one character to the left.
l moves the cursor one character to the right.
j moves the cursor down one line.
k moves the cursor up one line.
0 moves the cursor to the beginning of the line.
$ moves the cursor to the end of the line.
w move forward one word.
b move backward one word.
G move to the end of the file.
ggmove to the beginning of the file.
`. move to the last edit.
x remove the character on which you have your cursor.
u undo your last operation
Ctrl-r will redo the last undo.
d starts delete operation
d0 will delete to the beginning of a line.
d$ will delete to the end of a line.
dggwill delete to the beginning of the file.
dG will delete to the end of the file.
y yank text into the copy buffer.
p paste text after the current line.
P paste text on the current line.
v highlight one character at a time.
V highlight one line at a time.
Ctrl-v highlight by columns.
You may have noticed that several commands combine a text operation and movement key. gg takes you to the end of a file, and d is used to delete. Combining them gives you something more powerful.
Searching and Replacing
Now that you know how to enter text and are able to make changes etc, it is time to learn how to search for text and how to replace text. It is pretty easy.
Let's take an example, you want to search for expertpk in a file, simply press "/" and then the word or phrase you want to search, in our case expertpk. Type like below in command mode and hit Enter.
Wow, if this word is present in your file, vim will move your cursor to that word or phrase. Now if you want to search the same word again simply press "n" and vim will move forward to next word if exist. If you want to look backward, simply press "N" and vim will find the word in the opposite direction and move cursor to opposite direction.
Now, what if we want to start our search to upward. Simply type the below command and hit Enter
Wow, if this word is present in the file, vim will move the cursor to that word upward, again we can press "n" or 'N" to look for next word in the same direction or opposite direction.
Let's take an example, we have a word "Dog" in our file several times and we wish to replace it with "Cat". type the following command and press enter.
Vim will search "Dog" in the whole file and replace it with "Cat" without asking any question.
What if I should confirm this operation before replacement, Eg I want to replace 'Dog' in some locations only. Simply type below command and hit enter.
Now vim will search through the entire file and confirm before replacement operation.
Copying and Pasting
Copying and pasting are also very simple in VIM. 1st thing 1st, The last text that you've deleted with dd or
Now, what if I want to select text in command mode, I will press v and then using arrow keys or standard movement keys eg h, k, j, l I will highlight required text. Now I will press y to yank text into the copy buffer then move my cursor to the desired location and press "p" or "P" as per my requirements to paste this text.
You can see that v is a little slow if I wish to copy multiple lines or columns, I can use "V" to highlight the entire line, or even Ctrl +v to highlight entire column.
Vim's insert mode is quite simple, press "i" in command mode and you will be in insert mode, you can see the word "INSERT" in the bottom left corner. In this mode, all alphanumeric keys are unbound and now when we press any key eg if we press "d" it will insert character "d" in our file at cursor's location instead of starting the delete operation. Huh, very simple. You can now type any text with these keys and when you finish typing and want to exit from INSERT mode, you need to press ESC key only and superb, you are again in command mode. You wish to enter in INSERT Mode again, press "i" again and want to exit again, press ESC key again.
Note: Remember that in insert mode you can not use any alphanumeric key for command operation, e.g in insert mode you can not delete a line with dd but instead, it will insert dd in your file.
Last Line Mode:
Last line mode is another important mode. In this mode, you can run some most important commands in vim e.g save file, or quit from editor or search & replace etc. You can enter in last line mode only from command mode. To enter in command line mode you will type ":" in command mode and and and. only ":" will tell
Let's take an example of list line mode. If you wish to save your file simply type the below command. It will run the command in Last line mode.
And what if you want to save and quit at the same time. type below command and hit enter.
And what if you want to simply quit from the editor. Press below command and hit Enter.
Vim is your friend, if you had done some editing it will not pop you out destroying your changes, it will not save the file and ask you that "no write since last change,".
What I can do if I wish to quit without saving my changes. Very simple, use below command to override the default behavior of vim.
Note: You can also exit from vim by pressing ZZ, which will save and quit the file.
For now, it is enough for a beginner to learn the basics of vim. Keep practicing make you an expert and I assure you, you will never leave vim because it will increase your editing speed much more than any other editor.
Thanks for reading this article.
I am looking forward to your comments and advice. Please share this topic on social media to help us.