A Beginner's Guide To Using Vi/vim Editor

A Beginner's Guide To Using Vi/vim Editor

A Step by Step Guide to Setting up and Using vi/vim Editor

Play this article

What is vi/vim

Vi/Vim is a text editor that can be used in the command line. What makes this text editor very popular amongst developers is because When software developers/engineers work on servers, they often only have access to command-line editors, and in the command line, most 3rd party code editors like Atom, VS Code, etc. won't work. This is where terminal-based editors like vi/vim, Emacs, Less, Nano, etc. come into play. Additionally, vi/vim has two modes: Command Mode and Editor Mode, which makes it versatile and efficient for developers/engineers to write code and scripts. It is called vi (Visual Editor) or vim (Vi IMproved). originally, it was developed to work on UNIX-based operating systems, but as of now, it can also be installed as a standalone application on Windows Operating System.

How to Set up vi/vim

on most UNIX-based Operating Systems, like Linux (Ubuntu). the vi editor comes pre-installed, but for the Windows Operating system, the vi can be downloaded and installed as a standalone application, you can download it from the link below:

Download Vi/Vim for Windows Operating System

while for those wanting to use the vi improved (vim), on Ubuntu you will have to install it using the Terminal, by running the command:

sudo apt install vim

vim will be installed once the command runs successfully, and make sure your computer is connected to the internet.

Modes of Operation

We can't talk about using vi/vim without talking about the two modes of operation the vi/vim editor operates on. The two modes of operation are the:

  1. The Command Mode

  2. The Insert Mode

The Command Mode

This mode is the default mode of the text editor. It is the mode that is used to execute various commands that cause different actions to be taken on a file. In the command mode, every character typed does something to the file and these characters range from certain keys on the keyboard or a combination of different keys along with the Ctrl, Alt or Shift key. While in this mode, some keys might cause the editor to enter the Insert Mode, so in the case where you mistakenly enter the Insert mode, you can easily exit back to the command mode using the Esc key.

The Insert Mode

This mode can be entered from the command mode by pressing the key i on the keyboard. It is in this mode that text can be entered in a file. Every character you type here is added to the text in the file. After entering text into the file using the insert mode, one can go back to the command mode by using the Esc key on the keyboard.

How To Use the Vi/Vim Editor

Unlike the 3rd party and GUI (Graphical User Interface) based editors such as VS Code, Atom etc. that make use of the computer mouse to move around and perform some actions like moving the cursor, selecting the text etc.

In this Command line-based editor, the mouse is almost not of use, you have to make use of your keyboard constantly to carry out commands and perform various functions. Here I will talk about the commands that are most often used, starting from the basic ones down to the advanced ones. Also, make sure you are in command mode to be able to carry out most of the operations below.

Start and Exit vi/vim

  • To Start the vi/vim

    Use the command: vi filename for vi, and vim filename for vim. After running this command on the terminal, the file with the name filename will be open if it exists, in a situation where a file with that name does not exist, it will create a file with that exact file name. The filename can be replaced here with any name that you wish to name your file with.

  • To Exit the vi/vim

    You can use the following commands, depending on your needs:

  1. :x + Enter This saves the latest changes made to the file and exits.

  2. :wq + Enter This also saves the latest changes made to the file and exits the editor, it does the same work as the :x + Enter

  3. :q + Enter This command quits or exits the editor, in the case where some changes were made, it will alert you, giving you the option to either save the latest changes first before you exit or to exit without saving the latest changes that you made to the file.

  4. :q! + Enter This command forces the editor to quit even when the latest changes made to the file have not been saved.

Navigating the Editor

Just like I mentioned earlier, the mouse has almost no use in the vi/vim editor and the reason is that when this editor was developed, it was done with the QWERTY keyboard in mind. Another thing that can be used to move around is the arrow keys on the keyboard, which can be used to move left, right, up and down while in the editor. However, sometimes these arrow keys give some strange effects, so it is advisable to make use of the various keys from the keyboard, and they are:

  1. j or the down-arrow key moves the cursor down by 1 line.

  2. k or up-arrow key moves the cursor up by 1 line.

  3. h or left-arrow key moves the cursor left by 1 character.

  4. l or right-arrow key moves the cursor right by 1 character.

  5. 0(zero) key moves the cursor to the beginning of the line on which the cursor is presently on.

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

  7. w key moves the cursor to the beginning of the next word.

  8. b key moves the cursor to the beginning of the preceding word.

  9. :0 moves the cursor to the first line in the file.

  10. :n moves the cursor to the line number, n, where n can be any number.

  11. :$ moves the cursor to the last line of the file.

Adding, Changing and Deleting Text

  • Adding Text

    1. i insert text before the cursor.

    2. I insert text at the beginning of the current line.

    3. a insert/append text after the cursor.

    4. A insert/appends text at the end of the current line.

    5. o puts the text in a new line below the current line.

    6. O puts the text in a new line above the current line.

  • Changing Text

    1. r changes the character presently under the cursor.

    2. R changes the character under the cursor from the point the R is pressed until The Esc key is pressed.

    3. cw changes the current word with new text until The Esc key is pressed.

    4. cNw changes N (number) of words starting from the word under the cursor until The Esc key is pressed. For example, c3w changes three words starting from the word that is under the cursor.

    5. C changes/replaces all characters in the current line starting from the character under the cursor until The Esc key is pressed.

    6. cc replaces the entire line irrespective of the position of the cursor, until The Esc key is pressed.

    7. Ncc or cNc replaces N number of lines starting from the line the cursor is until Esc is hit. For example, 3cc or c3c replaces three lines starting from the line the cursor is presently on.

  • Deleting Text

    1. x deletes a single character under the cursor.

    2. Nx deletes N number of characters, starting from the one under the cursor. For example, 8x deletes 8 characters, starting from the one under the cursor.

    3. dw delete the single word under the cursor.

    4. dNw deletes N number of words starting from the word under the cursor. For example, d5w deletes 5 words starting from the word under the cursor.

    5. D delete the remainder of the line, starting with the line under the cursor.

    6. dd deletes the entire line on which the cursor is presently on.

    7. Ndd or dNd deletes N number of lines starting from the line the cursor is positioned. For example, 8dd or d8d deletes 8 lines from the line the cursor is positioned.

Copying, Cutting and Pasting Text

  • Copying Text

    1. yy copies the current line with the cursor.

    2. Nyy or yNy copies N number of lines, starting from the line with the cursor. For example, 3yy or y3y copies 3 lines, starting with the line that has the cursor.

  • Cutting Text

    1. dd cuts the entire line with the cursor.

    2. dw cuts the single word under the cursor.

    3. dNw cuts N number of words starting from the word under the cursor. For example, d5w cuts 5 words starting from the word under the cursor.

    4. D cuts the remainder of the line, starting with the line under the cursor.

    5. Ndd or dNd cuts N number of lines starting from the line the cursor is positioned. For example, 8dd or d8d cuts 8 lines from the line the cursor is positioned.

  • Pasting Text

    To paste a text that has been copied or cut, simply use the p key.

Searching Text and Determining Line Number

  • Searching Text

    1. /str searches for the occurrence of the word str going forward from the position of the cursor.

    2. ?str searches for the occurrence of the word str going backward from the position of the cursor.

    3. n moves forward to the next occurrence of the searched word/string.

    4. N moves backward to the next occurrence of the searched word/string.

  • Determining Line Number

    1. :.= returns the line number with the cursor at the bottom of the screen.

    2. := returns the total number of lines in the file at the bottom of the screen.

    3. Ctrl + g this command carries out the two commands above at the same time, that is it returns the line number with the cursor and also the total number of lines in the file at the bottom of the screen.

    4. :set number This command shows the number of each line at the beginning of the lines in the file you are working in.

Saving (Writing) and Reading Files

This command helps one to inputs and output files other than the file you are working on. For instance, you can bring in the content of a file into the file you are presently working on without leaving the present file you are working on, and in some instances, you can also save the current file you are working on as another file (i.e with a different name) without leaving the present file you are working on. Below are some of these commands:

  1. :r filename While inside a file, this command helps you bring in the content of the file named filename into the present file you are working on. It brings it in after the line with the cursor.

  2. :w writes/saves changes made to the present file you are working on.

  3. :w filename Writes the contents of the present file you are working on to a new file named filename. The filename will be created automatically and the contents will be copied into the file.

  4. :n, Nw filename Writes the content of the present file you are working on in the editor starting from line number n to line number N to a new file named filename. This file will be created automatically. For example, 5, 15w Gideon will write the content of the file I am presently working on in the vi/vim editor to a new file that will be named Gideon.

  5. :w! filename This command writes the contents of the present file one is working on in the editor to an already existing file named filename.

Conclusion
VI/VIM is a powerful text editor used by many software engineers and developers because of how simple it is and how easier it is to navigate while working on it. However, other command line-based editors are also very powerful and are used by many developers all over the world, and some of these editors are; Emacs, Nano, less etc. You can also comment by adding to the list some which you know of that are not mentioned here.