Skip to main content

Vi & Vim editor

D. Balakrishna<br/> Research Associate, IIITH
Introduction to Vi
Vi is the most commonly available screen editor found in almost every version of UNIX and Linux distributions. It is the only editor that is available in every UNIX installation. The vi editor is developed around 1976 by Bill Joy. It is very small, in fact, some versions have a total code size is less than of 100KB. This makes it easy to include Vi in small versions of Linux distributions for embedded systems. It is very powerful as just a few very short commands can make changes to large document. Vi has a total of approximately 150 basic commands. However, it is only necessary to know about a dozen of commands in order to be able to use it effectively.

Vi has two basic modes of operation.
  1. Command mode
  2. Insert mode

Command Mode:
Command mode is the default mode when a file is opened. Because every file is opened initially in command mode, you can not immediately begin typing text, that is everything that is typed on the keyboard is interpreted by vi to be a command. You can perform many types of commands on a file in command mode and they are:
  • Switching to text insert mode
  • Moving the cursor around the file
  • Deleting characters or lines
  • Transposing characters
  • Appending the content of the file to another file
  • Saving the file to disk
  • Setting vi options
  • Closing or quitting the file

Insert Mode:
Insert mode also referred to as text insert mode or input mode. It is used for entering the text into the file (memory). In this mode, everything typed on the keyboard is added to the file and does not become a command. The most common way to switch from command mode to insert mode is press ‘i’ command (which stands for insert mode) on the keyboard. Now you are ready to enter the text into the file. When you are finished entering the text or you want to perform some other operation such as moving to a different position in the file then press ‘ESC’ in order to return to the command mode.

To start Vi editor, open terminal and type vi filename at the command line and you will see a screen like the below:

For example: Vi file.txt

After opening this file, you have to enter ‘i’ command on keyboard, so that it move insert mode and now you enter text into the file as shown in below figure.
Engineering
Once you entered some text in the file, you can use four commands for moving the cursor around the file to modify the text from desired location. Using these commands you can modify the text including insertions and deletions. The four cursor positioning commands are:
  • ‘h’ -- move the cursor one character to left
  • ‘j’ -- move the cursor one line down
  • ‘k’ -- move the cursor one line up
  • ‘l’ -- move the cursor one character to right

Each of these commands can be either used by itself or modified by typing an integer in front of it to indicate the number of character or lines to move.

Examples:
  • 4h -- will move the cursor four characters left to it.
  • 5j -- will move the cursor five lines down.
  • 7k -- will move the cursor seven lines up.
  • 8l -- will move the cursor eight characters right to it.

After editing text in the file you have to save the file, for this you have to come out from insert mode to command mode by pressing ‘ESC’ command from keyboard. Now enter ‘:’ from keyboard which will be shown you in the last line of the file and then by giving two commands you can quit the file with saving or without saving as shown in below figure.
  • :q -- quit
  • :q! -- quit without save
  • :x -- save and quit

Engineering

Vi basic commands list
Entering into command mode:
  • ‘ESC’ -- exit editing mode

Moving the cursor:
  • ‘h’ or left arrow key – move the cursor left.
  • ‘l’ or right arrow key – move the cursor right.
  • ‘j’ or down arrow key – move the cursor down.
  • ‘k’ or up arrow key – move the cursor up
  • Ctrl + ‘f’ – move the cursor one page forward.
  • Ctrl + ‘b’ – move the cursor one page backward.
  • ‘^’ – move the cursor to the first non-whit character in the current line.
  • ‘$’ – move the cursor to the end of current line.
  • ‘G’ – move the cursor to the last line in the file.
  • ‘nG’ – move the cursor to the line number n.

Entering into editing mode:
  • ‘i’ – insert new text before the cursor.
  • ‘a’ – append new text after the cursor.
  • ‘0’ – start to edit a new line after the current line.
  • ‘O’ – start to edit a new line before the current line.

Replacing the characters, lines and words:
  • ‘r’ – replace the current character.
  • ‘s’ – enter edit mode and substitute the current character by several characters.
  • ‘cw’ – enter edit mode and change the word after the cursor.
  • ‘c’ – enter edit mode and change the rest of the line after the cursor.

Copying and pasting:
  • ‘yy’ – copy the current line to the copy or paste buffer.
  • ‘p’ – paste the copy or paste buffer after the current line.
  • ‘P’ – paste the copy or paste buffer before the current line.

Deleting characters, words and files:
  • ‘x’ – delete the character at the cursor location.
  • ‘dw’ – delete the current word.
  • ‘dd’ – delete the current line.
  • ‘D’ – delete the remainder of the line after the cursor

Repeating commands:
  • ‘.’ – repeat the last insertion, replacement or delete command.

Looking for strings:
  • ‘/string’ – find the first occurrence of the string after the cursor.
  • ‘?string’ – find the first occurrence of the string before the cursor.
  • ‘n’ – find the next occurrence in the last search.

Replacing strings:
  • ‘n,ps/str1/str2/g’ – between line numbers n and p substitute all occurrences of str1 by str2.
  • ‘1,$s/str1/str2/g’ – in the whole file substitute all occurrences of str1 by str2.

Exiting and saving:
  • ‘zz’ – save current file and exit vi.
  • ‘:w’ – save the file without quitting.
  • ‘:wq’ – save the file and quit.
  • ‘q!’ – quit vi without saving changes.

Introduction to Vim
Vim is text editor that is upward compatible to Vi and written by Bram Moolenaar in 1991. Vim is designed for both command line interface and a standalone graphical text editor. Vim basic commands are very simple but in combination of simple commands become very powerful. Vim has two basic modes.
  • Insert mode – In which you write text as if in normal text editor.
  • Command mode – which provides you efficient ways to navigate and manipulate text.

At any time you can see which mode you are in on the status bar which is located at the top of the editor. To change between modes can be done by using ‘ESC’ for command mode and ‘i’ for insert mode. To open a file for editing with Vim, simply type vim filename in the terminal then you can see blank file as shown in the following figure which is in insert mode.
Engineering
  • As normal text editors, you can use keys ‘h’, ‘j’, ‘k’, ‘l’ instead of arrow keys to move cursor to modify the text in the file.
    • ‘h’ – move the cursor left.
    • ‘l’ – move the cursor right.
    • ‘k’ – move the cursor up.
    • ‘j’ – move the cursor down.

  • To navigate the text in terms of words, you can use keys w, b, and e, moving within the text is not limited to individual keys, and you can combine movement with numbers.
    • ‘w’ – moves to the start of next word.
    • ‘e’ – moves to the end of the word.
    • ‘b’ – moves to the beginning of the word.
    • ‘$’ – moves to the last character of the line.
    • ‘0’ – moves to the first column of the line.
    • ‘W’ – moves to next word by ignore punctuation.
    • ‘B’ – moves to word beginning by ignoring punctuation.
    • ‘E’ – moves to word end by ignoring punctuation.
    • ‘^’ – moves to the first non blank character of the line.

Editing:
Inserting text into file is very simple in Vim by just typing ‘i’, it is entered into the insert mode and start typing the text into file.

Undo and Redo:
Don’t afraid to try the various commands, you can do undo almost anything by typing ‘u’ in the command mode.

Searching and Replacing:
Searching in Vim is very easy. Type ‘/’ in the command mode and enter the word you want to search for, and Vim will search the file in forward direction and highlight the results. Use ‘n’ or ‘N’ to follow through search results.

Exiting and Saving:
Once you are done editing and ready for save the type ‘ESC’ which will take to into command mode and use the following for saving and quitting the file.
  • ‘:w’ – save the file.
  • ‘:wq’ – save the file and quit the editor.
  • ‘:q’ – quit the editor without saving the file but it will give warning that modified file has not been saved.
  • ‘:q!’ -- quit the editor without saving the file and no warning.
Published date : 28 Jul 2015 03:34PM

Photo Stories