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

CommandDescription
kMove one line up
jMove one line down
hMove one character left
lMove one character right
bLeft one word
wRight one word
0Move 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

CommandDescription
iStart text entry mode
xDelete one character
ddDelete a line
dwDelete a word
rReplace a character
ROverwrite 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

CommandDescription
ZZWrite(if changes are made) then quit
:wqWrite, then quit
:qQuit(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

CommandDescription
/stringSearch forward (top to bottom) for text
?stringSearch backward (bottom to top) for text
yyCopy the current line
5yyCopy 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

CommandDescription
:set nuWill displays the line number
:set nonuWill hide line number
:set wm=nSet the margin n spaces from the right side of the screen
:set wm=0Turn 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

CommandDescription
ctrl+fMoves forward one screen
ctrl+bMove backward one screen
ctrl+dMove down (forward) one half screen
ctrl+uMove up (back) one half screen
ctrl+lRedraw the screen
ctrl+rRedraw 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.

Leave a Comment

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