Unix Tutorial :vi editor Part -1



Last updated on November 12th, 2016 at 05:42 am

vi editor is a visual text editor. It is pronounced as V and I editor. Here we would be learning how to use the vi editor, how to exit vi editor. vi shows you part of your file and allows you to enter commands that change something (add new stuff, delete a char or line, etc).

vi has a couple of modes:

command mode: move the cursor around, move to a different part of the file, issue editing commands, switch to insert mode. This mode enables you to perform administrative tasks such as saving files, executing commands, move the cursor around, move to a different part of the file, issue editing commands, switch to insert mode. In this mode, whatever you type is interpreted as a command.

insert mode: whatever you type is put in the file (not interpreted as commands).

when you first start vi you will be in command mode.

how to exit vi editor

Now we would be seeing the command for How to start the vi editor,how to exit vi editor, How to manipulate text in editor etc

When Starting VI

vi filename Edits filename or open up a new file  example vi x.sh
vi -r filename Edits last save version of filename after a crash
vi + n filename Edits filename and places curser at line n  example vi +2 x.txt
vi + filename Edits filename and places curser on last line example vi + x.txt
vi +/string filename Edits filename and places curser on first occurance of string
vi filename file2 … Edits filename, then edits file2 … After the save, use :n

how to exit vi editor

 

ZZ or :wq or 😡 Saves and exits VI
:w Saves current file but doesn’t exit
:w! Saves current file overriding normal checks but doesn’t exit
:w! file Saves to file overriding normal checks but doesn’t exit
:n,mw file Saves lines n through m to file example :2,10w j.txt
:n,mw >>file Saves lines n through m to the end of file
:q Quits VI and may prompt if you need to save
:q! Quits VI and without saving .If you have done some mistake and want to quit without making any changes
:e! Edits file discarding any unsaved changes (starts over)
:we! Saves and continues to edit current file

Inserting Text

i Insert before cursor
I Insert before line
a Append after cursor
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters
:r file Reads file and inserts it after current line
:nr file Reads file and inserts it after line n

Cursor Movement

 

h Move left
j Move down
k Move up
l Move right
w Move to next word
W Move to next blank delimited word
E Move to the end of Blank delimited word
0 or | Move to the beginning of the line
n| Moves to the column n in the current line
$ Move to the end of the line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file .Suppose you want to move to 100th line then 100G
:n Move to nth line of the file
H Move to top of screen
nH Moves to nth line from the top of the screen
M Move to middle of screen

Deleting Text

D Delete to the end of the line
d$ Deletes from the cursor to the end of the line
dd or :d Delete current line
ndw Deletes the next n words starting with current
ndb Deletes the previous n words starting with current
ndd Deletes n lines beginning with the current line. Suppose we want to delete 100 lines then 100dd
:n,md Deletes lines n through m
“np Retrieves the last nth delete (last 9 deletes are kept in a buffer)

Yanking Text

yy Yank the current line
:y Yank the current line
nyy or nY Places n lines in the buffer-copies

Putting text

p Put after the position or after the line
P Put before the position or before the line

Please look at the below articles for further information on Vi editor

vi tutorial Part-2

vi tutorial part-3

Hope you like the content on How to start the vi editor,how to exit vi editor, How to manipulate text in editor etc. Please do provide us the feedback to improve the content or if you need any thing which is missing


Leave a Reply