vi editor
Start vi editor
From the linux bash shell:
vi - classic text editor in Linux family
vim - vi improved. vi-like editor with more advanced functions
Once you are inside the editor, press i to insert text.
To save the contents to a file name test-file:
:w test-file.txt
Open an existing file in vi editor
vi test-file.txt
If a file does not exist, you can use the input mode to insert content and save the file with :w
See the colon mode section for more options in colon mode
Input mode commands
Press i for insert mode
Esc to get out of insert mode
Any of these commands leaves vi in input mode until you press Esc.
a Append after cursor
i insert before cursor
o open line below
O open line above
Non input mode
u undo
x erase character
r replace character
^ begining of line
$ end of line
w forward word by word
b backword word by word
Colon mode
Press Shift key followed by : to enter colon mode
:w name - write edit buffer to file name
:wq - save file and quit
:w enter - save and continue editing
ZZ - save and quit
:q! - quit without saving changes
:set number display line numbers
:2,4y yank lines 2 to 4
:2,4d delete lines 2 to 4
Search
:/abc - search test "abc"
:/ - repeat above search
:?abc - search "abc" backwards
:? - repeat search backwards
Search and replace
Change every occurrence of “foo” with “bar”
:%s/foo/bar/g
Change every occurrence of “foo” with “bar”, but ask for confirmation first
:%s/foo/bar/gc
Press ESC to exit colon mode
Block mode
you will need to exit insert mode(with Esc) for these commands to work
yy yank
yy p insert yanked line at the bottom of file
yy P insert yanked lines at the begining
dd delete current line
dG delete from current line till end of line
dgg delete all lines above current line
dw delete a word
Delete a block
Go to the starting line and type ma (mark "a"). Then go to the last line and enter d'a (delete to mark "a").
Cut and paste:
See “Delete a block” section to delete block. Move the cursor to the position where you want to insert the deleted block.
p insert deleted block after cursor
P insert deleted block before cursor
Copy and paste
Go to the beginning of block and type ma (mark "a").
Then go to the end of block and enter y'a (yank to mark "a").
p - lower case p to insert yanked block after cursor
P - upper case p to insert yanked block before cursor
Indent Indent 3 lines (current line plus 2 lines below)
3>>
Scrolling
ctrl D - scroll down one half screen
ctrl U - scroll up one half screen
ctrl B - scroll down one screen
ctrl F - scroll up one screen
ctrl E - scroll down one line
ctrl Y - scroll up one line
Visual Block
create a visual block
ctrl v
Lets you select a block using the cursor. You can use d and yy commands on the selected block
Example of visual block usage:
Add # comment character to a block of lines (of shell script)
Go to first line you want to comment, press Ctrl V, and select until the last line
Press I#Esc (then give it a second), and it will insert a # character on all selected lines.
Un-comment a block
put you cursor on the first # character, press Ctrl V, and go down until the last commented line and press x. That will delete all the # characters vertically.
Visually indent a block
To adjust the indent:
Put the cursor anywhere in the first line.
Press V then down arrow to select multiple lines
Press > to indent (shift text one 'shiftwidth' to the right)
Press V select lines and press < to shift left.
Press . to repeat the indent, or u to undo if you have shifted too far.
Type gv if you want to reselect the lines (that are not needed).