VI Material
VI Material
Introduction
Vi is a screen editor for Linux, Unix and other Unix-like operating
systems. Pronounced (vee-aye), vi stands for visual instrument. It is a widely-used default text
editor for Unix-based systems and is shipped with virtually all versions of Unix. It exclusively
uses the keyboard and provides a very efficient interface for editing programs and scripts.
Vim (Vi Improved) as its name suggests, is a clone of Vi and offers yet
more features than Vi. It’s free and open source, designed for use both from a command-line
interface and as a standalone application in a graphical user interface (GUI).It’s highly
configurable and comes with notable features such as syntax highlighting, mouse support,
graphical versions, visual mode, many new editing commands and a large amount of extension
plus much more.
Vi is a text editor originally created for the Unix operating system. Instead
of menus, vi uses combinations of keystrokes in order to accomplish commands. As such,
there is no need for the use of a mouse or a touchpad — everything is done exclusively
with the keyboard.
o Command Mode: In command mode, actions are taken on the file. The vi editor starts
in command mode. Here, the typed words will act as commands in vi editor. To pass a
command, you need to be in command mode.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key
will take you to the command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on
the screen but you'll be in insert mode. Now you can type anything.To exit from insert mode
press Esc key, you'll be directed to command mode.
Vi Commands
vi /home/sssit/Downloads/file.txt
Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist)
and have entered the full path for the directory Downloads.
Command mode
This is what you'll see when you'll press enter after the above command. If you'll start
typing, nothing will appear as you are in command mode. By default vi opens in
command mode.
Look at the above snapshot, it is blank as it is a new file. To start typing, you have to
move to the insert mode. At the end of the terminal window, directory name and file
name are displayed.
Look at the above snapshot, after pressing i we have entered into insert mode. Now we
can write anything. To move to the next line press enter.
Once you have done with your typing, press esc key to return to the command mode.
Command Action
Look at the above snapshot, command: wq will save and quit the vi editor. When you'll
type it in command mode, it will automatically come at bottom left corner.
If you want to quit without saving the file, use :q. This command will only work when
you have not made any changes in the file.
Look at the above snapshot, this file is modified and hence on typing :q it displays this
message at bottom left corner.The above file can be saved with the command :!q. It
discards the changes made in the file and save it.
Look at the above snapshot, we have typed :!q, it will save our file by discarding the
changes made.
exit vi table:
Commands Action
:w Save
:q Quit
To exit from vi, first ensure that you are in command mode. Now, type :wq and press
enter. It will save and quit vi.
i
The i command allows us to write before the current character.
Example:
Look at the above snapshot, our cursor is at e. We forgot to write word command before
word example. Hence, here we can use i command..
Example:
Look at the above snapshot, we want to write word Hello at the starting of the line.
Look at the above snapshot, after pressing I, we have typed Hello at the starting of the
line.
A
The A command allows us to start typing at the end of the current line.
Example:
Look at the above snapshot, we want to move at the end of the current line.
Look at the above snapshot, after pressing A, we have written one more line
o
The o command allows us to start typing a new line after the current line.
Example:
Look at the above snapshot, we are at the middle of the line and want to start a new line.
Look at the above snapshot, after pressing o, we arrived at next line and typed there.
O
The O command allows us to start typing on a new line before the current line.
Example:
Look at the above snapshot, we are at the middle of the line and want to write before the
current line.
Look at the above snapshot, after pressing O, we have written in a new line before the
current line.
j
The j command works same as the downward arrow. You can move to the below
lines.
Example:
Look at the above snapshot, letter e is repeated twice. To correct that we need to go to
that line.
Look at the above snapshot, after pressing either j key or downward key, we moved to
that line and corrected it.
k
The k command works same as the upward arrow. You can move to the above lines.
Example:
Look at the above snapshot, our cursor is at letter e and we want to move up at the blank
line to write something.
Look at the above snapshot, after pressing either k key or upward key, we moved to the
upper line and have written over there.
h
The h command works same as the leftward arrow. You can move in leftwards direction.
Example:
Look at the above snapshot, our cursor is at the end and we want to move to the starting
of the second line.
Look at the above snapshot, after pressing either h key or leftward key, we're at starting
of the second line.
l
The l command works same as the rightward arrow. You can move in rightwards
direction.
Example:
Look at the above snapshot, we are at starting and we want to move to the end of the last
line
Look at the above snapshot, after pressing either l key or rightward arrow, we're at the
end of the last line.
nG
Here n denotes a number. You can type a number before G to jump to a particular line.
Example:
7G
Look at the above snapshot, our cursor is at line 1 and we want to go to the line 7.
Look at the above snapshot, in command mode after typing 7G (you'll not be able to see
the typed command 7G on the screen), we're at 7th line.
G
If you'll not type any number before G, then you'll be directed to last line of the file.
Look at the above snapshot, after pressing G, we're at the last line of the file.
``
This command allows us to move to the previous position in the file.
Example:
Look at the above snapshot, we are at line 5 but forgot to write something on line 9.
Look at the above snapshot, after typing `` we moved back to line 9.
x
The command x is used to delete current character.
Example:
Look at the above snapshot, our cursor is at letter d and we want to delete it.
Example:
Example:
Look at the above snapshot, our cursor is at character e. Word teh is wrongly spelt. To
correct it r can be used.
Look at the above snapshot, both the characters that is character at the cursor (e) and
character at the right of the cursor (h) are switched from their positions. Hence now
word teh becomes the.
dd
The command dd deletes current line completely.
Example:
Look at the above snapshot, our cursor is at line third. We want to delete this whole line.
Look at the above snapshot, after pressing dd command, third line is deleted.
D
The command D is used to delete from current character to the end of the line.
Example:
Look at the above snapshot, our cursor is at third line at letter a. We want to delete from
the current character till the end of the line.
Look at the above snapshot, after pressing D, all the characters are deleted from letter a to
the end line.
dG
The command dG is used to delete from current line to the end line of the file.
Example:
Look at the above snapshot, all the lines are deleted from second line to the end line.
u
This command allows us to undo or cancel our last command.
Example:
Look at the above snapshot, with the help of dG command we have deleted last lines.
Look at the above snapshot, after pressing u, last command dG disables and all the
content came back
(.)
The . command allows us to repeat last used command.
Example:
Look at the above snapshot, we have used D command to delete some part of line second.
yy : Copy
The command yy (yank yank) is used to copy a line. Move the cursor to the line you want
to copy and then press yy.
P : Paste
The p command paste a copied or cut content after the current line.
Example:
The P command paste a copied or cut content before the current line.
Example:
Look at the above snapshot, after copying the content using yy, we moved the cursor to
first line and then pressed P. Line is copied before the current line
ndd
Here n denotes a number. It will delete n number of lines after the current line.
Example:
2dd
Look at the above snapshot, this is our script and we want to delete first two lines.
Look at the above snapshot, after typing 2dd, two lines after the current lines are deleted.
nyy
The command nyy is used to copy n number of lines after the current line.
Example:
2yy
Look at the above snapshot, we want to copy two lines after the first line.
Look at the above snapshot, after pressing 2yy, last two lines are copied and with P key
we have pasted them before the first line.
0
The 0 sign bring you to the start of the current line.
Example:
Look at the above snapshot, currently our cursor is at the middle of the second line.
Look at the above snapshot, after pressing key 0, we're at the starting of second line.
The ^ sign also bring you at the start of the current line.
$
The $ sign will bring you to the end of the current line.
Example:
Look at the above snapshot, currently our cursor is at the middle of the second line.
Look at the above snapshot, after pressing $ key, we are at the end of the current line.
d0
Combining sign 0 with d command enables us to delete start of a line from current
character.
Example:
Look at the above snapshot, position of the cursor is somewhere in the middle of the line.
Look at the above snapshot, letters from starting to the current character are deleted
d$
Combining sign $ with d command enables us to delete from current character to the end
of the line.
Example:
Look at the above snapshot, position of the cursor is somewhere in the middle of the line.
Look at the above snapshot, letters from current character to the end of the line are
deleted.
J
The J command can be used to join two lines
Example:
Look at the above snapshot, we need to join first two lines of the above script.
Look at the above snapshot, after pressing J, both the lines are joined.
yyp
The yyp command repeat the current line.
Example:
Look at the above snapshot, our current line is the second line.
Look at the above snapshot, after pressing yyp command, current line is repeated.
ddpThe command ddp swaps two lines (current line and line below the current line).
Example:
Look at the above snapshot, last two lines are swapped from their positions.
w
The w command allows us to move one word forward. To move more than one word
forward, you can type number before w command.
Example:
9w
Look at the above snapshot, from the current word we want to move 9 words ahead.
Example:
9b
Look at the above snapshot, from the current word we want to move 9 words backwards.
Example 1:
Look at the above snapshot, we want to delete word example. Our cursor is placed at the
starting letter e.
Look at the above snapshot, after pressing dw command, word example is deleted.
Example 2:
Look at the above snapshot, here also we want to delete word example. But this time our
cursor is placed somewhere at the middle of the word.
Look at the above snapshot, after pressing dw command, word example is not completely
deleted. Only the letters starting from the cursor to the end of the word (including the
space) are deleted.
Example 3:
5dw
Look at the above snapshot, here we want to delete five letters after the current word.
Look at the above snapshot, after typing 5dw, five words after the cursor are deleted.
yw
The command yw is used to copy one word. Here also cursor has to be placed at the
starting to copy a complete word.
Example:
Look at the above snapshot, after pressing yw key, move the cursor at place where you
want to copy the word. Here, we have copied it at the last with p command
/string
Here string has to be replaced by the searching word.
The /string allows us to do forward search for a string. If the same string is placed before
and after the cursor, it will only indicate the string after the cursor. When you'll start
typing /string in command mode, at lower left corner, typed word will be seen.
Example:
/readable
Look at the above snapshot, this is our normal script. We have to search
word readable from this script.
Look at the above snapshot, after typing /readable and pressing enter, mentioned word
on its first occurence will be automatically highlighted. Please note here, first occurence
is in forward search. The string readable is also present before the cursor, but it searches
forward.
?string
The ?string allows us to do backward search for a string. If the same string is placed
before and after the cursor, it will only indicate the string before the cursor. When you'll
start typing ?string in command mode, at lower left corner, typed word will be seen.
Example:
/readable
Look at the above snapshot, this is our normal script. We have to search
word readable from this script.
Look at the above snapshot, after typing ?readable and pressing enter, mentioned word
on its first occurence will be automatically highlighted. Please note here, first occurence
is in backward search. The string readable is also present after the cursor, but it searches
backward.
/^string
This command searches a string in forward direction but at the beginning of a line. If no
such word is present at the beginning of the line, it will show a no match message.
Example:
/^readable
Look at the above snapshot, after typing /^readable, cursor moved to the string which is
at the starting of the line.
/string$
This command searches string in forward direction but at the end of a line. If no such
word is present at the end of the line, it will show a no match message.
Example:
/readable$
Look at the above snapshot, after typing /readable$, cursor moved to the string which is
at the end of the line.
Syntax:<startLine,endLine> s/<oldString>\/<newString>/g
Note: Here g stands for globally. If the string is repeating more than once in a single line,
then g is to be used to replace it globally from all the places in the mentioned line range.
Example 1:
:1,$ s/readable/changed/
Look at the above snapshot, we have written the command :1,$ s/readable/changed/
After pressing enter key, string readable is replaced with string changed.
:g/string/d
To delete a certain string from the entire file.
Example:
We want to remove the occurrence of the string Verilog in the above shown code which
can be done with the command :g/Verilog/d
You can see that the string Verilog in all its occurrences will be deleted.
To delete the string for a particular range of lines we can use the command
:<startLine,endLine> g/string/d
:<startLine,endLine> s/^/<modification>/
Used to do any modifications (like giving 2 character length space, adding extra characters
to the pre-existing line etc.) at the beginning of the line.
Example:
We want to add Subject- in the beginning of the line from line 1 to 4 which is done using
the command :1,4 s/^/Subject-/
You can see that the necessary modification has been made.
Similarly if we want to make any modifications to the end of the line for a particular line
range we can use the command :<startLine,endLine> s/$/<modification>/
:0
To go to the beginning of the file.
:$
To go to the end of the file.
Shift+*
To highlight a text's every occurrence in the file.
To move around a file:
Commands Action
j To move down
k To move up
h To move left
l To move right
To delete:
Commands Action
D Delete the current line from current character to the end of the line
Commands Action
Commands Action
dd Delete a line
Commands Action
Commands Action
Joining lines:
Commands Action
To jump lines:
Commands Action
Commands Action
Search a string:
Commands Action
/\<he\> Search for the word he (and not for there, here, etc.)
Syntax:
1. :<startLine,endLine> s/<oldString>/<newString>/g
Example:
Commands Action
:1,$ Replace forwa with backward from first line to the last line Commented [JS1]: @Naresh P It look like sentence
mentioned in action column is wrong as per command used
s/readable/changed/
:3,6 s/letters/neww/g Replace forward with backward from third line to the ninth
line
Text buffers:
Commands Action
Visual commands:
Commands Action
Commands Action
aw Mark a word
ib Inner ()block
iB Inner {}block
Commands Action
X Delete a character
Dw Delete a word
Dd Delete a line