How to use vim editor

If you are a Linux user then you already know that in Linux everything is written in the file. A device in Linux is represented as a file, for configuring a service you have to edit lots of files like a mail server or a web server, even firewall information is also written in a file which you can edit. So if you are an expert Linux user then this tutorial is not for you cause getting expertise in Linux require a simple skill which is editing a file with a file editor. It’s funny, but if you want to move in with Linux you have to know how to edit a file in Linux. there are lots of file editor exist in Linux. Among them vim is the most popular file editor. Today we will see how to use vim editor on Ubuntu. The process is also same in other Linux distribution. (Throughout this post i use : sign, which you need to type, otherwise the command won’t work.)

Open and exit from vim editor

To open vim editor, perform the following in a Terminal.

1. Type vim
2. Press Enter

You will see the default vim window with some information like version of the vim editor, how to exit from the editor, help etc. To exit from vim editor, perform the following

1. Type :q
2. Press Enter

Modes

The vim editor typically functions in three modes:
1. Command mode: When you first run vim filename to edit a file, vim starts out in command mode. For searching a text, you need to be in this     mode.
2. Insert mode: To enter the insert mode, type i. For entering text, you need to be in insert mode.
3. Last line mode: Once you press Escape key from insert mode you are in command mode again. Press : for last-line mode. For undo, replace you need to be in this mode.

The Escape key is your friend. If you got stuck, press the Escape key.

File Open

Open a file name myfile (or any other name you like) in vim editor.

vim myfile

You are in vim command mode. To write something in this file you need to switch to the insert mode. Type i for insert mode. After pressing i you will see a – – INSERT – – message at the bottom of your file.

Type anything in that file.

Now it’s time to save the file.

File Save

1. Press Escape (this will bring you to command mode)
2. Type :w
3. Press enter

The file will be saved and you are in command mode now. To write into the file again press i and you will be back in insert mode.

File Save and Exit

If you want to save the file and exit from the vim editor, then perform the following:

1. Press Escape (this will bring you to command mode)
2. Type :x
3. Press enter

The file will be saved and you will be exited form vim editor.

Exit from vim editor without saving

This step is really helpful.  Sometimes when you messed up with the file and don’t want to save, what you gonna do? Undo is a good options which we will see soon but an easy trick is just exit from the file without saving. This options is helpful for long configuration file.

1. Press Escape (this will bring you to command mode)
2. Type :q!
3. Press enter

Cursor movement

Cursor movement only happens in command mode. To move the cursor, press the h, j, k, l keys as indicated.

 

     ^
    k
< h                        l >
       j
       v

• The h key is at the left and moves left.
• The l key is at the right and moves right.
• The k key is at the up and moves up.
• The j key is at the down and moves down.
• The arrow keys will also work for cursor movement. If you want to move your cursor in insert mode, then use the arrow key.

Undo Redo

To undo the last change, perform the following

1. Press Escape (if you are not in the command mode)
2. Press :u 
3. Press enter (This will undo the last change you made)

To undo the undo’s, perform the following in command mode.

1. Press Escape (if you are not in the command mode)
2. Hold down the Ctrl key and press R

Copy Paste

Open your file in vim editor. There should be some text which you type earlier. You should be in command mode.

vim myfile

To copy paste operation, perform the following in command mode:

1. Place your cursor under the first character of the word you want to copy. For example, if you want to copy the word cricket, place your cursor under the character c. (Move your cursor using h,j,k,l keys or arrow keys)
2. Press v
3. Move your cursor to the end of the word. The area will be highlighted.
4. Press y (it will copy the text to buffer)
5. Press p (where you want to paste)

To copy and paste one line at a time, perform the following

1. Place the cursor at the beginning of the line you want to copy.
2. Press V (That’s a capital V) It will highlight the whole line.
3. Press y (it will copy the line to buffer)
4. Press p (where you want to paste)

Search and replace

Perform the following in command mode for search and replace operation

1. For searching a text press / in command mode
2. Type the text you want to search. For example, /cricket.
3. Press n to move the cursor to the next instance of the text from the last search. (Follow your cursor)
4. Press N to move the cursor to the previous instance of the text from the last search. (Follow your cursor)

Perform the following in last-line mode for replacing a text.

1 Type :%s/text/replacement_text/g (It will search through the entire document for text and replace it with replacement_text)
2 Type :%s/text/replacement_text/gc (It will search through the entire document for text and replace it with replacement_text and confirm before replacing the text)

Perform the following in command mode for replacing on character.

Example: 192.168.0.15 will be replaced by 192.168.0.16

1. Press Escape (if you are not in the command mode)
2. Move your cursor just below the number 5 (192.168.0.15).
3. Press r
4. Press 6 (192.168.0.16)

Delete

Perform the following for deleting one character.

 Press Escape (if you are not in the command mode)
2. Place your cursor under the character you want to delete.
3. Press d
4. Move your cursor. The character will be deleted.

Perform the following for deleting one word.

1. Press Escape (if you are not in the command mode)
2. Place your cursor under the first character of the word you want to delete.
3. Press dw. The word will be deleted.

Perform the following for deleting one line.

1. Press Escape (if you are not in the command mode)
2. Place your cursor under the line you want to delete.
3. Press dd. The line will be deleted.

Performing the following activity will not make you a vim expert but you will get some idea about vim editor. That’s all for today.

Thanks for reading the post. If you enjoyed the post, please share it with your network and let me know your thoughts in the comments. 

About the Author: Imtiaz is working in a financial organization in Bangladesh and having experience in system, network and security administration. Feel free to contact with him on LinkedIn or Twitter

 

Leave a Reply

Your email address will not be published. Required fields are marked *