<<Back to Linux Main Page
Frequent Used Linux vi Editor Commands
What is vi?
vi (visual editor) is a default editor in LinuxThe vi editor has 2 modes
1: command mode
2: insert mode
To open a file with vi, invoke vi command as shown below
vi <file_name>
If the <file_name> file does not exists vi will open a new named file.
After opening the file in vi editor just press i to inter into insert mode. In this mode, entered text is inserted into the file. To turn off the insert mode just press Esc (Escape) button on keyboard
Important vi commands
How to open a file using vi editor
vi <file_name> <=edit file starting at line 1vi -r <file_name> <=recover crashed file that was being edited
Example:
$ vi -r new_file1
How to close a file opened vi editor
When you wish to exit from vi editor, first turn off the insert mode just press Esc (Escape). Now you can type the command followed by : and press enter (i.e. return button on the keyboard).You can even combine multiple command eg. :wq (command :w writes the changes in the file to the disk and command :q quits vi)
:x <= Save the changes in file and quit vi
:wq <= Save the changes in file and quit vi
:w <= Save the changes in file
:q! <= discard the changes in file and quit vi
:q <= quit vi
NOTE: You can not quit if the changes in file is not saved, in that case either save the changes or force quit using :q! command
Moving the Cursor in vi
j or <Return key> or <down-arrow> <= move cursor one line downk or <up-arrow> <= move cursor one line up
h or <Backspace> or <left-arrow> <= move cursor one character left
l or <space> or <right-arrow> <= move cursor one character right
0 <= move cursor to start of current line
$ <= move cursor to start of current line
w <= move cursor to the beginning of next word
b <= move cursor back to beginning of preceding word
e <= move cursor to the end of next word
:0<Return> or 1G <= move cursor to first line in file
:n<Return> or nG <= move cursor to line n
:$<Return> or G <= move cursor to last line in file
NOTE: The symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed
Screen Manipulation Commands in vi
^f <= move forward one screen^b <= move backward one screen
^d <= move down (forward) one half screen
^u <= move up (back) one half screen
^l <= redraws the screen
^r <= redraws the screen, removing deleted lines
Inserting, Changing, and Deleting Text in vi
Reverting the Changesu <= undo
Inserting the text
i <= insert text before cursor
I <= insert text at beginning of current line
a <= insert text after cursor
A <= insert text to end of current line
o <= open and put text in a new line below current line
O <= open and put text in a new line above current line
Changing the text
r <= replace single character under cursor
R <= replace characters, starting with current cursor position
cw <= change the current word with new text,
cNw <= change N words beginning with character under cursor
C <= change (replace) the characters in the current line
cc <= change (replace) the entire current line
Ncc or cNc <= change (replace) the next N lines, starting with the current line,
Deleting the text
x <= delete single character under cursor
Nx <= delete N characters, starting with character under cursor
dw <= delete the single word beginning with character under cursor
dNw <= delete N words beginning with character under cursor;
D <= delete the remainder of the line, starting with current cursor position
dd <= delete entire current line
Ndd or dNd <= delete N lines, beginning with the current line;
Cutting and Pasting Text
yy <= copy (yank, cut) the current line into the buffer
Nyy or yNy <= copy (yank, cut) the next N lines, including the current line, into the buffer
p <= put (paste) the line(s) in the buffer into the text after the current line
Saving and Reading Files in vi
:r <= filename<Return> read file named <filename> and insert into current file after current line
:w<Return> <= save the content of the file
:w newfile<Return> <= Save the content of file to new file
:12,35w newfile<Return> <= write the contents of the lines numbered 12 through 35 to a newfile
:w! existingfile<Return> <= over write the existingfile with the content of current file
Other Important Commands
:set nu <enter> <= set the line numbers
:set nonu <enter> <= unset the line numbers
:.= <enter> <= returns line number of current line at bottom of screen
:= <enter> <= returns the total number of lines
^g <enter> <= provides the current line number, along with the total number of lines in the file
/string <= search forward for occurrence of string in file
?string <= search backword for occurrence of string in file
n <= move to next occurrence of search string
N <= move to next occurrence of search string in opposite direction
Search and Replace text in vi
:s/string <= search the string:s/string1/string2/ <= Search string1 and replace the first occurance with string2
:%s/pattern/replace/g <= Search string1 and replace all the occurances with string2
:%s/pattern/replace/gc <= Search string1 and replace all the occurances with string2 but ask for confirmation before replacing
:%s/\<string1\>/string2/gc <=Find and Replace Whole Word Only
:%s/unix/Linux/gi <= Find and Replace ignoring the case
:%s/UNIX/bar/gI <= Find and Replace case sensitive
:100,200s/UNIX/Linux/g <= find and Replace All Lines Between line 100 and line 200
Comments
Post a Comment