vi
Tutorial
vi
has two modes - command mode and insert mode.
In command mode, you are not directly typing text into the file.
Instead, you are giving commands like delete a single line, delete several lines, paste several lines, replace all instances of “hello
” with “howdy
”, save the file, quit, move the cursor, and so on.
At any time, to get into command mode, you hit the esc
key.
In insert mode, you can type normally.
There are a few ways to get into command mode:
i
: insert text at the cursora
: insert the text immediately following the cursoro
(lowercase letter): makes a new line for text after the cursorO
(uppercase letter): makes a new line for text before the cursorBelow is a set of very useful vi
commands which apply in command mode:
h
: move leftl
: move rightk
: move upj
: move downG
: go to the end of the file/hello
: go to the next instance of “hello
” in the filedd
: delete the current line of textp
: paste the last deleted text below the cursorP
: paste the last deleted text above the cursorJ
: tack next line to end of current lineNote that these all move into insert mode.
a
: insert after current cursor spoti
: insert before current cursor spotI
: insert text at the beginning of current lineo
(lowercase letter): insert blank line after cursorO
(uppercase letter): insert blank line before cursorNote the use of the colon (:
) before these commands.
:w
: saves the file:q
: quits the program:q!
: quits program without saving:wq
: saves the file and quits the programNote the use of the colon (:
) before these commands.
:%s/hello/howdy/g
: replaces all instances of “hello
” with “howdy
”:5,7s/hello/howdy/g
: replaces all instances of “hello
” in lines 5
through 7
with “howdy
”Prepared for Computer Science 64 by Prof. Diana Franklin, with slight adaptation by Kyle Dewey.