How To Use Vi Text Editor An Easy Guide

How To Use Vi Text Editor An Easy Guide


This post was last updated on September 14th, 2019 at 08:53 am

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 cursor around text, deleting text, saving the content of 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 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 text you will have to go into command mode.

Use the keys in command mode

kMove one line up
jMove one line down
hMove one character left
lMove one character right
bLeft one word
wRight one word
0Move cursor to the beginning of the current line
$Moves cursor to the end of current line

Note: On some Unix or Unix based platforms arrow keys are allowed to use for moving 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

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

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 file

Searching, copying and pasting text in vi

Use the following in command mode

/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

:set nuWill displays 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 terminal

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

Use in command mode

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.

Previous How To Compile And Run C Or C++ Code In Linux/Unix
Next File Ownership And Permissions In Linux/Unix

About author

You might also like

Uncategorized 0 Comments

Process Management In Linux/Unix

This post was last updated on September 15th, 2019 at 05:39 pmA process is an instance of a computer program that is currently running. A program can have multiple instances

Uncategorized 0 Comments

Understanding Users And Groups In Linux/Unix

Linux/Unix are multi-user operating systems that means different users can access the system at a time and can use system resources simultaneously. To access the resources of a system you

Uncategorized 0 Comments

How To Install Or Remove A Software Package In Arch Linux, Manjaro?

Different Linux distribution comes with different tools to manage software packages on it. Pacman-based Linux distributions such as Arch Linux, Manjaro, etc use the Pacman package manager to install, remove,


No Comments Yet!

You can be first to comment this post!

Leave a Reply