How To Use Vi Text Editor An Easy Guide

Introduction to vi

Vi is a command based & standard Unix text editor. It is fast and powerful you need not remove your fingers from your keyboard while editing a file in it. It has short commands to manipulate the text. And it is available for nearly all Unix like operating systems. Originally it was developed by bill joy in 1976. Later various versions are released with improvement in it.

Starting vi in your terminal

1. Open your terminal

2.Type vi filename.txt

  • If filename.txt exist in your system it will open with the content in it
  • Otherwise, it will create a new file with the name filename & .txt extension
    and opens in your terminal for editing

Vi Operating modes

So before start using vi as your text editor you should have some basic understanding of how it operates in different modes. The editor operates in the following two modes –

1. Command mode 

  • It is the default mode of the editor and  when you first start a file in vi it opens in command mode
  • In this mode commands are given to accomplish different operations on files like moving the cursor around text, deleting text, saving the content of the file, etc.
  • vi commands are case sensitive that means you can’t use uppercase letters
    instead of lowercase and vice versa.

2. Text or Insertion mode

  • This is the mode in which every character typed is added into the file
  • You can go into insert mode in many ways but have to exit with<esc> key

Moving cursor around text

You can use the following keys to move the cursor around the text. So before using these keys in vi to navigate around the text you will have to go into command mode.

Use the keys in command mode

Command Description
k Move one line up
j Move one line down
h Move one character left
l Move one character right
b Left one word
w Right one word
0 Move the cursor to the beginning of the current line
$ Moves cursor to the end of the current line

Note: On some Unix or Unix based platforms arrow keys are allowed to use for moving the cursor around the text 

Entering, modifying and changing text

The following keys or combination of keys can be used to enter, modify or change the inserted text

Use these keys in command mode

Command Description
i Start text entry mode
x Delete one character
dd Delete a line
dw Delete a word
r Replace a character
R Overwrite text

Exiting vi

Once you are done writing, reading, or modifying a file in vi you can exit by using the following combination of keys. And before that, you have to activate command mode for vi. And you just have to press enter after the commands starting with a :(colon)

Use these commands in command mode

Command Description
ZZ Write(if changes are made) then quit
:wq Write, then quit
:q Quit(work if there are no changes made in the file)
:q! Quit without saving the changes that are made into a file

Searching, copying and pasting text in vi

Use the following in command mode

Command Description
/string Search forward (top to bottom) for text
?string Search backward (bottom to top) for text
yy Copy the current line
5yy Copy 5 lines begin with the current line
p (in small) Paste that text after the cursor position
P (in capital) Paste that text before the cursor position

Set line number and wrap margin in vi

Some time it is very useful to using line number while writing text

Use in command mode

Command Description
:set nu Will displays the line number
:set nonu Will hide line number
:set wm=n Set the margin n spaces from the right side of the screen
:set wm=0 Turn off the wrap margin

Manipulating editor screen in the terminal

You can use the following window commands in vi editor to move up or down screen several times

Use in command mode

Command Description
ctrl+f Moves forward one screen
ctrl+b Move backward one screen
ctrl+d Move down (forward) one half screen
ctrl+u Move up (back) one half screen
ctrl+l Redraw the screen
ctrl+r Redraw the screen, removing deleted lines


Using vi editor

1.Open terminal type vi filename.txt

2. Now the file is open in command mode press i and start writing your text

3. You are currently in insertion mode press <esc> to switch to command mode Save Now press ZZ or:wq and enter to save the file.

Now it will return to the terminal by saving the contents of the file. I think the list of commands given above in the article is sufficient to start with it. You should try the commands on your own system so that you get more familiar with it.

3 thoughts on “How To Use Vi Text Editor An Easy Guide”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version