Monday 17 April 2017

All about the VI editor

The default editor that comes with the UNIX operating system is called vi (visual editor). It has two modes of operation:

1. Command mode commands which cause action to be taken on the file.
In the command mode, every single character typed is a command that performs operation on the text file being edited.

2. Insert mode in which entered text is inserted into the file.
In the insert mode, every character typed is added to the text in the file; pressing the Escape key turns off the Insert mode.

Syntax
vi file_name
It will open an empty file if not present already.

Commands that switch the editor to insert mode
a
It moves the cursor one position to the right before switching to insert mode, basically it will append
i
Insert text at current cursor position.
I
Insert text at the beginning of the current line.
o
Insert a blank line under the current cursor position and move the cursor to that line.
O
Insert a blank line above the current cursor position and move the cursor to that line.

Basic operations
Some of the popular vi commands are:

Moving through the text
h move the cursor to the left
l move the cursor to the right
k move the cursor up
j
move the cursor down
H
moves the cursor to the top line of the screen
M
moves the cursor to the middle line of the screen
L
moves the cursor to the last line of the screen
w
move the cursor forward one word
b
move the cursor backward one word (if you are in the middle of a word, b command will move you to the beginning of the current word)
e
move to the end of a word
(
move to beginning of current block
)
move to beginning of current block
0
move to the beginning of a line
$
move to the end of a line

Screen Movement
Ctrl-f
scrolls down one screen
Ctrl-b
scrolls up one screen
Ctrl-u
scrolls up half a screen
Ctrl-d
scrolls down a half a screen
SHIFT-G
put the prompt at the end of the file, Preceding G with a number will move you to a specific line in the file
1G
move to the beginning of the file

Delete
dw
deletes from the character selected to the end of the word
ndw
deletes n words at the right side of the cursor
dd
deletes the current line
ndd
deletes n lines starting from the current cursor position
D
deletes from the current character to the end of the line
x
deletes the character on which the cursor is positioned
nx
delete the character selected and the next (n-1) characters
X
deletes the character on left of your cursor
nX
deletes n character on left of your cursor
$-9,$d
deletes last ten lines

Copy, Paste, Replacement | Join and Undo
yw
copies a word into a buffer
nyw
copies n word into a buffer
yy
copies a line into a buffer
nyy
copies n line into a buffer
p
paste line or words copied earlier on the line below the cursor
p
paste line or words copied earlier on the line above the cursor
r
replaces the current character with the next character you type and returns to command mode
cw
Changes and replaces the current word with text that you type. Press ESC to get out of replacement mode
:1,$s/word/newword/g
word with newword throughout the file

J join the line on which cursor is placed with another line
nJ join n lines
:u undoes the last change you made in the file. Using it again will "undo the undo"
U undoes all recent changes to the current line


Display line number
:set number shows the line numbers
:n moves to line n of the file
:.= returns line number of current line at bottom of screen
:= returns the total number of lines at bottom of screen
^g provides the current line number, along with the total number of lines, in the file at the bottom of the screen

Search
/pattern search the string(pattern) in the file and position the cursor on the first match below its position
n search in a forward direction
N search in a backwards direction
?pattern search backward for occurrence of the pattern

Save and Quit
:w save the file
:q exit the editor
:q! forces the exit when you want to quit a file containing unsaved changes
:e! reads the original file back in so that you can start over
:wq save and exit
ZZ save file and exit VI
:w newfile
save the text to newfile
:wq! overrides read-only permission
:1,100w newfile write lines 1 to 100 to newfile
:$-9,$w newfile write the last ten lines to newfile (the dollar sign denotes last line)


Some more commands
:recover recover a file after an unexpected interruption
vi –r filename
R puts you in overtype mode until you press ESC
. Repeats the last command executed
:! command
execute external command from VI
:sh start a shell from VI, Return to VI by entering exit or ctrl d
:sp horizontally split current file
:vsp
vertically split current file
:r! command insert output of command to the file

No comments:

Post a Comment