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.


What's Vim?

    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:

  1. System administrator

  2. Programmer

  3. Heavy editor of plain text files

  4. Work in CLI mode (e.g ssh to remote host).


The Modes

Some people disagree on how many modes Vim actually has but following 3 modes are most important and you should learn about them.

  1. Command Mode

  2. Insert Mode

  3. Last Line Mode.


Are you ready to go next?

Let's install vim if you don't have by typing below command

In Debian:

apt -y install vim

In RMP

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 enough.

vim /var/log/syslog.1

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.

Command Mode:

    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:

  1. h moves the cursor one character to the left.

  2. l moves the cursor one character to the right.

  3. j moves the cursor down one line.

  4. k moves the cursor up one line.

  5. 0 moves the cursor to the beginning of the line.

  6. $ moves the cursor to the end of the line.

  7. w move forward one word.

  8. b move backward one word.

  9. G move to the end of the file.

  10. gg move to the beginning of the file.

  11. `. move to the last edit.

  12. x remove the character on which you have your cursor.

  13. u undo your last operation

  14. Ctrl-r will redo the last undo.

  15. d starts delete operation

  16. dw delete word.

  17. d0 will delete to the beginning of a line.

  18. d$ will delete to the end of a line.

  19. dgg will delete to the beginning of the file.

  20. dG will delete to the end of the file.

  21. y yank text into the copy buffer.

  22. p paste text after the current line.

  23. P paste text on the current line.

  24. v highlight one character at a time.

  25. V highlight one line at a time.

  26. 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. 

Searching:

    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.

/expertpk

    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

?expertpk

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.

Replacing:

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.

:%s/Dog/Cat/g

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.

:%s/Dog/Cat/gc

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 dw or x is stored in the buffer and ready to be pasted back into the document. So if you've run dd and deleted an entire line, you can now hit p or P to paste it back into the document. This goes for single lines, multiple lines, and even entire documents.

    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.

Insert Mode:

    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 vim that you are going to put a command in last line mode. Oops. Is that this much simple. Wow. 

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.

:w

And what if you want to save and quit at the same time. type below command and hit enter.

:wq

And what if you want to simply quit from the editor. Press below command and hit Enter.

:q

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.

:q!

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.