Vi commands: Simple and Quick tutorial [Linux]
VI is a display oriented interactive text editor originally created for the Unix operating system. It is available by default on all UNIX systems. It is supplied with all Linux distributions.
Here is a simple and quick VI command tutorial.
Note: VI commands are CASE SENSITIVE.
There are two modes in vi: command mode and insert mode.
All vi commands work on command mode.
– Hitting Esc will put you in command mode
– Hitting i and a in command mode will put you in insert mode.
– Hitting i will insert text before the cursor
– Hitting a will append text after the cursor
Creating new file
– Open terminal (In Ubuntu, press: CTRL + ALT + T)
– Type the following command to create new file named hello.txt:
vi -r filename = recover filename that was being edited when system crashed
– After you enter some text to your file, press Esc
– Then type “:w” (without quote)
– press Esc
– type “:q” (without quote)
:wq = save any changes and quit vi
ZZ = save any changes and quit vi
:q! = quit vi even though latest changes have not been saved
– h j k l will move cursor left, down, up, right respectively
– 0 (zero) will move cursor to start of current line
– $ will move cursor to end of current line
– w e b will move forward one word, forward to next end of a word, and back one word, respectively
– :0 (zero) or 1G will move cursor to first line in file
– :n or nG will move cursor to line n
– :$ or G will move cursor to last line in file
Moving through screen
CTRL f will move forward one screen
CTRL b will move backward one screen
x = delete single character
dd = delete entire current line
dw = delete single word beginning with character under cursor
db = delete word backward
D = delete the remainder of the line, starting with current cursor position
d$ = delete from cursor to end of line (same as D)
d0 = delete from cursor to beginning of line
dL = delete from current line to end of screen
dG = delete from current line of end of file
d) = delete complete sentence forward
d( = delete complete sentence backwards
Undo and Redo
u = undo last command
. (dot) = redo/repeat last command
Copy and Paste
yy = copy current line
yw = copy word forward
yb = copy word backward
Y = copy the remainder of the line, starting with current cursor position
y$ = copy from cursor to end of line (same as Y)
y0 = copy from cursor to beginning of line
yL = copy from current line to end of screen
yG = copy from current line to end of file
y) = copy from cursor to start of sentence
y( = copy from cursor to end of sentence
p (lowercase) = paste to the line below the current line
P (uppercase) = paste to the line above the current line
Lines and words deleted from dd and dw can also be pasted.
/string = search forward for occurence of string
?string = search backward for occurence of string
n (lowercase) = find the next occurence of the search
N (uppercase) = find the previous occurence of the search
Find and replace
:%s/foo/bar/g = replace all occurences of foo with bar in the entire file
s = substitute
g = global, i.e. the action should be performed for all instances of a string found on any given line
% sign = “Do this for all lines in this buffer.”
Other find and replace commands:-
:s/foo/bar/ = replace the first occurrence of the word foo on the current line with the word bar.
:s/foo/bar/g = replace all occurrences of the word foo on the current line with the word bar.
:%s/foo/bar/g = replace all occurrences of the word foo in the current file with the word bar.
:%s/foo//g = delete all occurrences of the word foo in the current file.
Reading and writing to file
– Open terminal
– Type vi and hit enter
– :r filename = read file named filename
– :w newfile = write the recently read content to newfile
:set nu = display line numbers
:set nonu = cancel line number display
:set ts=4 will set the number of spaces used to display the tab character to 4
:set ic = make string searches with / ignore cases
Example: After :set ic using /foobar will find foobar as well as FooBaR
We can execute any shell command inside vi itself.
:!command = execute command without leaving vi
Example: :!date = display date
We can start a new command shell inside vi itself.
:sh = start a new command shell inside vi
Type exit to leave that shell and return back to vi.