VI-EDITOR
Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables
you to edit lines in context with other lines in the file.
An improved version of the vi editor which is called the VIM has also been made available
now. Here, VIM stands for Vi IMproved.
vi is generally considered the de facto standard in Unix editors because −
• It's usually available on all the flavors of Unix system.
• Its implementations are very similar across the board.
• It requires very few resources.
• It is more user-friendly than other editors such as the ed or the ex.
You can use the vi editor to edit an existing file or to create a new file from scratch. You can
also use this editor to just read a text file.
Starting the vi Editor
The following table lists out the basic commands to use the vi editor −
Sr.No. Command & Description
1 vi filename
Creates a new file if it already does not exist, otherwise opens an existing file.
2 vi -R filename
Opens an existing file in the read-only mode.
3 view filename
Opens an existing file in the read-only mode.
Following is an example to create a new file testfile if it already does not exist in the current
working directory −
$vi testfile
The above command will generate the following output −
|
~
~
~
~
~
~
"testfile" [New File]
You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line.
If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or
some other non-viewable character present.
You now have one open file to start working on. Before proceeding further, let us understand
a few important concepts.
Operation Modes
While working with the vi editor, we usually come across the following two modes −
• Command mode − This mode enables you to perform administrative tasks such
as saving the files, executing the commands, moving the cursor, cutting
(yanking) and pasting the lines or words, as well as finding and replacing. In this
mode, whatever you type is interpreted as a command.
• Insert mode − This mode enables you to insert text into the file. Everything
that's typed in this mode is interpreted as input and placed in the file.
vi always starts in the command mode. To enter text, you must be in the insert mode for which
simply type i. To come out of the insert mode, press the Esc key, which will take you back to
the command mode.
Hint − If you are not sure which mode you are in, press the Esc key twice; this will take you
to the command mode. You open a file using the vi editor. Start by typing some characters and
then come to the command mode to understand the difference.
Getting Out of vi
The command to quit out of vi is :q. Once in the command mode, type colon, and 'q', followed
by return. If your file has been modified in any way, the editor will warn you of this, and not
let you quit. To ignore this message, the command to quit out of vi without saving is :q!. This
lets you exit vi without saving any of the changes.
The command to save the contents of the editor is :w. You can combine the above command
with the quit command, or use :wq and return.
The easiest way to save your changes and exit vi is with the ZZ command. When you are in
the command mode, type ZZ. The ZZ command works the same way as the :wq command.
If you want to specify/state any particular name for the file, you can do so by specifying it after
the :w. For example, if you wanted to save the file you were working on as another filename
called filename2, you would type :w filename2 and return.
Moving within a File
To move around within a file without affecting your text, you must be in the command mode
(press Esc twice). The following table lists out a few commands you can use to move around
one character at a time −
Sr.No. Command & Description
1 k
Moves the cursor up one line
2 j
Moves the cursor down one line
3 h
Moves the cursor to the left one character position
4 l
Moves the cursor to the right one character position
The following points need to be considered to move within a file −
• vi is case-sensitive. You need to pay attention to capitalization when using the
commands.
• Most commands in vi can be prefaced by the number of times you want the
action to occur. For example, 2j moves the cursor two lines down the cursor
location.
There are many other ways to move within a file in vi. Remember that you must be in the
command mode (press Esc twice). The following table lists out a few commands to move
around the file −
Given below is the list of commands to move around the file.
Control Commands
The following commands can be used with the Control Key to performs functions as given in
the table below −
Given below is the list of control commands.
Editing Files
To edit the file, you need to be in the insert mode. There are many ways to enter the insert
mode from the command mode −
Sr.No. Command & Description
1 i
Inserts text before the current cursor location
2 I
Inserts text at the beginning of the current line
3 a
Inserts text after the current cursor location
4 A
Inserts text at the end of the current line
5 o
Creates a new line for text entry below the cursor location
6 O
Creates a new line for text entry above the cursor location
Deleting Characters
Here is a list of important commands, which can be used to delete characters and lines in an
open file −
Sr.No. Command & Description
1 x
Deletes the character under the cursor location
2 X
Deletes the character before the cursor location
3 dw
Deletes from the current cursor location to the next word
4 d^
Deletes from the current cursor position to the beginning of the line
5 d$
Deletes from the current cursor position to the end of the line
6 D
Deletes from the cursor position to the end of the current line
7 dd
Deletes the line the cursor is on
As mentioned above, most commands in vi can be prefaced by the number of times you want
the action to occur. For example, 2x deletes two characters under the cursor location
and 2dd deletes two lines the cursor is on.
It is recommended that the commands are practiced before we proceed further.
Change Commands
You also have the capability to change characters, words, or lines in vi without deleting them.
Here are the relevant commands −
Sr.No. Command & Description
1 cc
Removes the contents of the line, leaving you in insert mode.
2 cw
Changes the word the cursor is on from the cursor to the lowercase w end of the word.
3 r
Replaces the character under the cursor. vi returns to the command mode after the
replacement is entered.
4 R
Overwrites multiple characters beginning with the character currently under the cursor.
You must use Esc to stop the overwriting.
5 s
Replaces the current character with the character you type. Afterward, you are left in the
insert mode.
6 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.
Copy and Paste Commands
You can copy lines or words from one place and then you can paste them at another place using
the following commands −
Sr.No. Command & Description
1 yy
Copies the current line.
2
yw
Copies the current word from the character the lowercase w cursor is on, until the end of
the word.
3 p
Puts the copied text after the cursor.
4 P
Puts the yanked text before the cursor.
Advanced Commands
There are some advanced commands that simplify day-to-day editing and allow for more
efficient use of vi −
Given below is the list advanced commands.
Word and Character Searching
The vi editor has two kinds of searches: string and character. For a string search,
the / and ? commands are used. When you start these commands, the command just typed will
be shown on the last line of the screen, where you type the particular string to look for.
These two commands differ only in the direction where the search takes place −
• The / command searches forwards (downwards) in the file.
• The ? command searches backwards (upwards) in the file.
The n and N commands repeat the previous search command in the same or the opposite
direction, respectively. Some characters have special meanings. These characters must be
preceded by a backslash (\) to be included as part of the search expression.
Sr.No. Character &Description
1 ^
Searches at the beginning of the line (Use at the beginning of a search expression).
2 .
Matches a single character.
3 *
Matches zero or more of the previous character.
4 $
End of the line (Use at the end of the search expression).
5 [
Starts a set of matching or non-matching expressions.
6 <
This is put in an expression escaped with the backslash to find the ending or the beginning
of a word.
7 >
This helps see the '<' character description above.
The character search searches within one line to find a character entered after the command.
The f and F commands search for a character on the current line only. f searches forwards
and F searches backwards and the cursor moves to the position of the found character.
The t and T commands search for a character on the current line only, but for t, the cursor
moves to the position before the character, and T searches the line backwards to the position
after the character.
Set Commands
You can change the look and feel of your vi screen using the following :set commands. Once
you are in the command mode, type :set followed by any of the following commands.
Sr.No. Command & Description
1 :set ic
Ignores the case when searching
2 :set ai
Sets autoindent
3 :set noai
Unsets autoindent
4 :set nu
Displays lines with line numbers on the left side
5 :set sw
Sets the width of a software tabstop. For example, you would set a shift width of 4 with
this command — :set sw = 4
6 :set ws
If wrapscan is set, and the word is not found at the bottom of the file, it will try searching
for it at the beginning
7 :set wm
If this option has a value greater than zero, the editor will automatically "word wrap".
For example, to set the wrap margin to two characters, you would type this: :set wm = 2
8 :set ro
Changes file type to "read only"
9 :set term
Prints terminal type
10 :set bf
Discards control characters from input
Running Commands
The vi has the capability to run commands from within the editor. To run a command, you only
need to go to the command mode and type :! command.
For example, if you want to check whether a file exists before you try to save your file with
that filename, you can type :! ls and you will see the output of ls on the screen.
You can press any key (or the command's escape sequence) to return to your vi session.
Replacing Text
The substitution command (:s/) enables you to quickly replace words or groups of words within
your files. Following is the syntax to replace text −
:s/search/replace/g
The g stands for globally. The result of this command is that all occurrences on the cursor's
line are changed.
Important Points to Note
The following points will add to your success with vi −
• You must be in command mode to use the commands. (Press Esc twice at any
time to ensure that you are in command mode.)
• You must be careful with the commands. These are case-sensitive.
• You must be in insert mode to enter text.
there are total 4 modes in vim editor
1. command mode: when we first open a file then it is opened in command mode. in
command mode we cannot edit the file. we can do different types of task like copy, paste,
delete, undo, redo, change mode things.
dd- delete a line.
5dd-delete 5 line downwards from cursor point.
yy-to copy a line
p- to paste the line
5p-5 time it will paste.
5yy-5 line downwards from the cursor point will be copid.
u-undo
ctrl+r-redo
dw-to delete a line but cursor must be in the begining of the word.
shift+zz --> to save and quit from the editor in command mode.
2. insert mode/ edit mode: to edit a file we need to go to insert mode. to open insert mode we
can use the following char.
a-it will let you edit the file from the next char.
A-it will help us to edit the file from the end of the line.
i-where the cursor is present from that point it will let you edit.
I-it will let you edit the file from the begining of the file.
o-it will open a new line downside where cursor is present.
O-it will insert a new line upside where cursor is present.
to go back from insert mode to command mode we have to use Esc button.
to save and quit shift+zz
3. last line mode/advanced command mode: to save data and also copy,paste, delete, create
new file, encryption
all kind of task can be done from this mode. to go in last line mode we have to use Esc, shift
+:
all the command in last line mode we will use after pressing Esc, shift + :
:set nu- show line no of each line.
:set nonu- remove line no of each line.
:4,8co32 - to copy 4-8 line and paste on 32 no line.
:4,8m15 - to cut 4-8 and paste on 15 no line.
:1,15w /filepath - to create a new file with 1-15 no line of the current file.
:w! - save the data in vim/vi editor but don't quit.
:q! - without saving data it will quit the editor.
:wq! - it will save the data and quit from the editor.
:X - to set a password and encrypt all the data in that file.
:X then dont give any password just press enter two time. it will remove the password and
encryption.
4. visual mode: normally not used. if we want to include a char on multiple line than we use
that mode.
VIM
i-- edit from the cursor point
I-- begening of the line
a -- from the next character
A -- from the end of the line
o -- new line down
O -- new line up
there are 4 modes:
1. command
we copy,paste,delete lines
2. insert(edit the file)
3. last line mode/advanced command mode(we do copy paste move but with line no, in order
to save the data we need this mode)
4. visual mode( not used much)( for changing multiple line at a time, for searching word in a
file)
1. to go back to command mode use "Esc"
dd-- to delte a line
yy-- to copy a line
p -- paste a line
5dd -- to delete 5 line delete from the cursorpoint downside.
5yy -- copy 5 lines
5p- paste line 5 times
for undo -- 'u'
for redo -- ctrl+r
3. shift+: to go to last line mode
. -- repeat the last command
j --come downwards
k -- go upwards
l -- go rightside
for saving and quiting from the editor shift+: wq!
saving shortcut "shift+zz"
to encrypt a vim file shift+: X
give password twice
for decrypting
shift+:X
enter
enter
save and quit