vi Manual


Published: 2015-04-29
Updated: 2015-05-11
Web: https://fritzthecat-blog.blogspot.com/2015/04/vi-manual.html


You surely have already tried a lot of text editors in your life. But do you know the oldest of all? If you are using UNIX or LINUX, or WINDOWS with CYGWIN, you already got it on your machine. When not, here is its download page:

Vim

This is the latest materialization of vi. There is even a Java fork of it: jvi.

But why should we care about such exotic old and complicated editors?

Might be useful in some situations ...

vi is an UNIX editor that runs in terminal mode, meaning you do not need a graphical environment to use it, just the curses library. Mind that you won't ever see a scrollbar with vi, and it does not have a menu, and it (most likely) won't respond to mouse-clicks!

vi has two modes:

  1. command mode
  2. edit mode

In command mode you can not enter or change text, you just can launch commands to start doing such.
In edit mode you can write new text.
You get into edit mode by pressing certain keys like 'i' or 'a' (insert, append, see reference below).
From there you can return to command mode by pressing ESCAPE.

So when you use vi, and you want to think about what you just have written, type ESCAPE, else you will forget that you are in edit mode and use the cursor keys, and this leads to control-characters in text!

To save data and finish the editor, you need ex-commands. These start with a colon ':' from command mode, and are committed by typing ENTER. ex was the predecessor of vi.
For example, you normally quit vi by typing : x ENTER. But you can also launch any shell command, like e.g. a look at the current directory, by : ! ls -l ENTER.

Starting and Terminating

Start to edit one or more files by

vi filepath [filepath2 filepath3 ...]

Initially you will be in command mode. Then use following ex-commands.

Input Semantic
:q! Dismiss data and quit editor
(quit without ! would be denied when data have been modified)
:x Save and quit
:w Save text, but do not quit
:w filepath Write text to given filepath
:f Display name of currently visible file
:n Go to next loaded file
:rew Return to first loaded file (rewind)
:e filepath Load another file
:set all Display all current settings
:set encoding=Cp1252
:set number
:set nonumber
:set autoindent
:set noautoindent
Set the encoding to Cp1252,
set line numbers,
unset line numbers
set automatic indentation,
unset automatic indentation

Moving the Cursor

Normally the Cursor Keys and Page-Up / Page-Down should work.
When not, use the following keys from command mode (press ESCAPE before).

Input Semantic
h Left one character
j Down one line
k Up one line
l Right one character

Here are further commands to move around in file (press ESCAPE before).

Input Semantic
w Move one word forward
b Move one word backward
^ Move to start of line
$ Move to end of line
1G Move to first line (2G = move to 2nd line, ...)
$G Move to last line
Ctrl-f Page down (forward)
Ctrl-b Page up (backward)

Editing

Inserting

All the following keys are typed in command mode, and they lead to edit mode.
When you type ENTER in edit mode, you will continue inserting text in a new line.
Other than ESCAPE, ENTER does not terminate edit mode.

Input Semantic
i Insert at current cursor position
I Insert at start of current line
a Append after current cursor position
A Append at end of current line
o Create empty line above current line
O Create empty line below current line
ESCAPE Finish edit mode, return to command mode

Modifying

Like insert and append, following keys are typed in command mode and lead to edit mode.

Input Semantic
cw Change word at current cursor position
c$ Change text from current cursor position until end of line
cc Change the whole line

Useful modification commands that do not lead to edit mode:

r Replace character at cursor position with next input character
J Join current line with line below.
To split a line, go to split-position and type i ENTER.
>> Indent one tab
<< Un-indent one tab

Deleting

Following commands delete text in command mode, and they stay in command mode.

Input Semantic
x Delete character at cursor position (2x = delete 2 chars, ...)
dw Delete word at cursor position
d$ Delete text from cursor position until end of line
dd Delete current line (2dd = delete two lines, ....)
.,$d
1,.d
Delete all lines from current to last line
Delete all lines from first to current line

Undo and Redo

Typing u is Undo, and typing u again is Redo :-)

Input Semantic
u Undo the recent change
:undo Undo the recent change
:redo Redo the recent change
. Repeat the previous command at current cursor position, whatever it was

Cut, Copy & Paste

Using the Default Buffer

When you delete text, the deleted text is automatically copied into default buffer.
You can insert the content of that buffer anywhere by typing p (print).
To copy text into the default buffer, use y (yank).

Copy & Paste of a word:


(move cursor to start of word to copy)
yw
(move to insert position)
p

Cut & Paste of a word:


(move cursor to start of word to cut)
dw
(move to insert position)
p

Copy & Paste of a text line:


(move to line to copy)
yy
(move above insert line)
p

Cut & Paste of a text line:


(move to line to copy)
dd
(move above insert line)
p
Input Semantic
yw Copy word at cursor position into default buffer (yank word)
yy Copy current line into default buffer (2yy = copy two lines, ....)
.,$y
1,.y
Copy (yank) all lines from current to last line
Copy all lines from first to current line
p Insert default buffer contents at current cursor position or line (print)

Copy & Paste of a text block:


(move cursor to start of block to copy)
ma
(move to end of block to copy)
y'a
(move to line above target position)
p

Cut & Paste of a text block:


(move cursor to start of block to copy)
ma
(move to end of block to copy)
d'a
(move to line above target position)
p
Input Semantic
ma Set the mark with name 'a' onto current line, which can be accessed then by 'a
y'a Copy lines between current line and mark 'a' into default buffer
d'a Cut lines between current line and mark 'a' into default buffer

Using Named Buffers

Unlike most modern editors, vi provides more than one text buffer for Copy & Paste.
You can give them names, fill them with text, and then use them by prepending "b to commands (assuming the buffer was named 'b').

Copy & Paste of a text block using a named buffer:


(move cursor to start of block to copy)
ma
(move to end of block to copy)
"by'a
(move to line above target position)
"bp

Cut & Paste of a text block using a named buffer:


(move cursor to start of block to copy)
ma
(move to end of block to copy)
"bd'a
(move to line above target position)
"bp
Input Semantic
"byw Copy current word into buffer with name 'b'
"bdw Delete current word into buffer with name 'b'
"byy Copy current line into buffer with name 'b'
"bdd Delete current line into buffer with name 'b'
"by'a Copy lines between current line and mark 'a' into buffer with name 'b'
"bd'a Delete lines between current line and mark 'a' into buffer with name 'b'
"bp Insert contents of buffer 'b' at current cursor position or line

Search and Replace

Searching

Forward search is done by typing '/' in command mode, backward search by '?'.
You can use regular expressions in the search pattern.

Input Semantic
/name Downward-search for 'name' in current file, starting from current line
?name Upward-search for 'name'
n Goes to next occurrence of recent search pattern
:set ignorecase Makes the search case-insensitive
:set noignorecase Makes the search case-sensitive

Replacing

Search and replace is done with an ex-command.
You must declare a line range for the replacement. 1,$ declares the whole file.
You can use regular expressions in the search pattern (text after first slash).

Input Semantic
:1,$s/word/WORLD/g From line 1 to last line, search 'word' and replace it by 'WORLD' (g = globally, all occurrences in a line)

Epilogue

vi is an editor for the skilled. Once you used it for a while you will appreciate it for its quick facilities. Unfortunately good documentation about this great editor is not easy to find.





ɔ⃝ Fritz Ritzberger, 2015-04-29