If you are starting your journey with Vi or Vim Editor, then you have reached the right place. 

 

Vi Editor is one of the most widely used text editors which can be accessed through command line of all machines installed with Unix operating systems or all linux based operating systems such as Ubuntu, Debian, Fedore, Red Hat, etc.

 

Vi works in a similar fashion across all operating system therefore once you have mastered Vi or VI Improved or VIM, you would be able to put your knowledge to use immediately everywhere.

 

Since the Vi Editor is command line based, you will be interacting with it through typing commands which would in turn makes you highly efficient.

 

To interact with Vi Editor, you need to understand the Terminology of Vi world. 

 

In Vi Editor, there is something called modes. Consider them as an environment where you get into.

 

There are two modes in Vi Editor -

 

1. Command Mode - That's the default mode in which you can access the contents of the file that you have opened. You can use all the commands that Vi Editor supports to go through the file, edit, copy and paste contents within the file. Commands in Vi Editor are case-sensitive so use commands with caution.

 

2. Insert Mode - It can be entered into by pressing "I or i" key. The Vi Editor in bottom left side will start showing INSERT informing the user that the current mode is indeed Insert mode. You can exit the Insert mode by pressing Esc key.

 

How to launch Vi Editor?

To launch Vi Editor, just go to the terminal or command line software in your operating system and type vi followed by the file name that you want to open. If the file doesn't already exists you can type vi followed by the file name that you want to assign to the new file.

 

Example - vi example.php will either open the file beingskilled.php or create a file named beingskilled.php depending on if the file called beingskilled.php already exists in the path or not.

 

How to exit from Vi Editor?

Exitting from Vi Editor is made meme about as it is interesting to learn about it. 

If you don't want to make any changes to the file that you have opened or might have accidentally made some changes which you don't want to save, press Esc to go to command mode and then type :q. It prompts you that you haven't saved the changes - that's basically preventing you from accidentally exiting the file without saving the changes.

 

If you still want to exit without saving the changes that you have made, then this time type :q! and you will be able to exit immediately. 

 

However, if you do want to save the changes, then press Esc key which will take you to Command Mode and further type :wq -- this will save the changes that you have made.

 

How to move cursor in Vi Editor? 

As mouse doesn't work in Vi Editor and it is not advisable to use arrow keys either, there are multiple keys that you need to remember which would help you in moving across the document.

 

Moving the cursor left or right - 

To move towards left by one character, press h key and to move towards right by one character, press l key.

 

Moving the cursor by one line up or down - 

To move the cursor up by one line, press j key and to move the cursor down by one line, press k key.

 

Moving the cursor along the current line - 

To move the cursor towards the begging of the current line, press 0 (zero) key and to move the cursor towards the end of the current line, press $ key.

 

Moving the cursor word by word - 

To move the cursor to the next word, press w key and to move the cursor to the preceeding word, press b key.

 

Moving the cursor line wise - 

To move the cursor to the starting or first line, type :0 (colon zero), to move the cursor towards the end line, type :$ (colon dollar symbol) and to move the cursor to a specific line, type :line-number (for example - if you wish to move the cursor to 5th line, type :5 (colon five) in command mode.

 

How to manipulate text in Vi Editor? - Adding, Modifying, Removing contents from the document - 

To be able to add, modify and remove any text, you will need to remember few keys as mouse can't be used for pointing to a particular location. 

 

How to add text in Vi Editor? 

To add the text before the cursor can be done by pressing 'i' key and after the cursor by pressing 'a' key. Once done, press Esc key.

To add the text in beginning of current line, press 'I' key and to add text to end of current line, press 'A' key. Once done, press Esc key.

 

How to modify text in Vi Editor?

Once the text is there in document, you may want to modify it. Once any of the command keys are pressed, you will enter the Insert mode as you are replacing the text. 

  • To just replace the word below cursor, press 'r' key and if you want to start replacing from the cursor then press 'R' key. Once done, press Esc key.
  • To modify the current word starting from where the cursor is, press 'cw' keys and the current word gets modified until the Esc key is pressed. Text beyond the cursor gets removed and you will be able to complete the word.
  • Similarly, if you want to modify n no. of words from the current cursor location, press 'cXw' keys where X is the no. of words that you want to modify. For example, if you want to modify next 6 words, type c6w and 6 words will be removed the current cursor's location.
  • To change all the characters in the current line from the position of cursor, press 'C' key and contents beyond the cursor will get removed.
  • To change the entire current line, press 'cc' keys and the entire line will get removed and you will be able to start writing the new text. 
  • To change the current line along with next N lines, press 'Ncc' or 'cNc' keys where N is the no. of lines you want to replace including the current line.

 

How to delete text in Vi Editor?

  • To delete the character under the cursor, press 'x' key.
  • To delete N no. of characters from the character under the cursor, press 'Nx' keys.
  • To delete the word starting the character below the cursor, press 'dw' keys.
  • To delete N no. of words starting the character below the cursor, press 'dNw' keys.
  • To delete rest of the current line starting the character below the cursor, press 'D' key. You will still be in Command mode in this case.
  • To delete the complete current line, press 'dd' keys. If you wish to delete N no. of lines, simply type 'Ndd' or 'dNd' where N is no. of lines. 

 

How to copy or paste text in Vi Editor?

  • If you wish to copy a line to memory or buffer, press 'yy' keys. Press 'p' key to paste the line from memory/ buffer to the line after/ below the current line.
  • If you wish to copy N no. of lines including the current line, type 'Nyy' or 'yNy' keys. Press 'p' key to paste the line from memory/ buffer to the line after/ below the current line. 

 

How to undo changes that you had made in Vi Editor?

  • You can undo the last change done by pressing 'u' key.
  • You can undo all the changes made to the current line by pressing 'U' key.

 

How to search any word or string in Vi Editor? 

  • To search for a string in the document, firstly you have to be in command mode by pressing Esc key.
  • Then for making a forward search -  type "/string or word" and then press 'Enter' key.  For example - to search forward the term "beingskilled" in the document, type /beingskilled 
  • For making a backward search - type "?string or word"  and then press 'Enter' key. For example - to search backwards the term "beingskilled" in the document, type ?beingskilled 
  • To navigate to the next occurence in the document, press 'n' key
  • To go to the next occurence backwards, press 'N' key.

 

How to see line numbers in Vi Editor?

When you are working in a document using Vi Editor, you would want to know the line number so that you can easily go to the specific line.

To see the current line number that will be shown in the bottom left corner, type ':.='  or : . = keys without spaces. 

To know the number of lines in the current file in Vi Editor, type := keys.