You are on page 1of 6

INTRODUCTION TO VI EDITOR

Introduction to vi editor:
There are many ways to edit files in Linux, editing files using the screen-oriented text editor vi is one
of them.
You can use the vi editor to edit an existing file or to create a new file.You can also use this editor to
just read a text file.
An improved version of the vi editor “VIM” is also available. VIM stands for Vi Improved; it is same as
vi, but has more features.

A few reasons why vi is one of the commonly used editors:


- It is usually available on all flavors of Linux distributions.
- Its implementations are very similar across the board.
- It requires very few resources.
INTRODUCTION TO VI EDITOR

vi uses 3 modes:
Command Esc
Insert ilaA
Line :

A few basics:
- Create a newfile vi filename
- Modify file i for insert mode and start typing
- Add a new line o
- Copy and paste a line yy (to copy) and p (to paste)
- Undo u
- Redo ctrl R
- Go to first and last line gg (go to first line) and G (last line)
- Use line mode “:nu” to see line number - /text to search for text(n to find next)
- Substitute a word :%s/is/was/g (will substitute ‘is’ with ‘was’ globally)
- Save and quit wq!
INTRODUCTION TO VI EDITOR

Moving within a File:


To move around within a file without affecting your text, you must be in the command mode (by
pressing the Esc key).

The following table lists out a few commands you can use to move around one character at a time:
k
Moves the cursor up one line
j
Moves the cursor down one line
h
Moves the cursor to the left one-character
l
Moves the cursor to the right one-character
INTRODUCTION TO VI EDITOR

Deleting Characters:
The following can be used to delete characters and lines in an open file:
x
Deletes the character under the cursor location
X
Deletes the character before the cursor location
dw
Deletes from the current cursor location to the next word
d^
Deletes from the current cursor position to the beginning of the line
d$
Deletes from the current cursor position to the end of the line
dd
Deletes the line the cursor is on
INTRODUCTION TO VI EDITOR

Change Commands:
You can also change the characters, words, or lines in vi without deleting them:
cc
Removes the contents of the line, leaving you in insert mode.

cw
Changes the word the cursor is on from the cursor to the lowercase w end of the word.

r
Replaces the character under the cursor. vi returns to the command mode after the replacement is entered.

R
Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the
overwriting.

s
Replaces the current character with the character you type. Afterward, you are left in the insert mode.

S
Deletes the line the cursor is on and replaces it with the new text. After the new text is entered, vi remains in the insert
mode.
INTRODUCTION TO VI EDITOR

Copy and Paste Commands:

You can copy lines or words from one place and paste them at another place using the following
commands:

yy
Copies the current line.
yw
Copies the current word until the end of the word.
p
Puts the copied text after the cursor.
P
Puts the yanked text before the cursor.

You might also like