vi

From NetBSD Wiki

Jump to: navigation, search
This article is a stub. You can help by editing it.

vi(1) is a powerful screen oriented text editor usable for programming, as well as quick editing of configuration files. It is part of the NetBSD base system, which means you don't have to install it. It is normally the default editor used by other tools for editing tasks. This can be changed by setting the $EDITOR environment variable.

Nowadays, vi really is a family of programs, which include the all-popular vim (vi iMproved), the cool Elvis and the sarcastic vigor. All of these and more are available from pkgsrc. The vi that is installed with NetBSD's base system is actually nvi, by Berkeley hacker Keith Bostic. It has less features than many of the more popular vi clones, but the most important are there:

  • Unlimited undo (see Shell-Hacks), something which a lot of people think it doesn't have just because vim does it slightly different.
  • Tab-completion
  • History editing of command given on the : line
  • "smart" indenting

Unfortunately a lot of these things are not enabled by default, which is the main reason many people install other vi clones from pkgsrc, with nicer defaults.

Contents

Invoking and getting out

To start editing a file, simply:

vi filename

This will replace the content of your terminal (or terminal window) with the file content. The line at the bottom of the terminal is a status/information area, where useful information, like file name and cursor position is written.

To quit the editor without saving, write:

:q!

To save your edits enter:

:w

To save and quit editing enter:

:wq

or:

:x

These commands are called colon commands and are written at the status line as you type them.

Editing and navigating

Insert mode

When started, vi is in Command Mode, also called Normal mode. When in this mode, all keystrokes are commands to vi and will not be inserted into the file, unlike in most other editors. Before entering text, you have to switch to Insert mode. These are some of the commands for switching to insert mode:

i insert before the current character
a insert after the current character
I insert at beginning of current line
A insert at end of current line

ESC can be used to leave Insert mode, returning to Command mode.

Movement commands

There are several commands used to move the cursor:

h one character left
l one character right
j one line down
k one line up
w to start of next word
b to start of current word or previous word if already at start
0 to start of line
$ to end of line

If the TERM environment variable is correctly set, the arrow keys can be used instead of h, j, k and l.

Replacing text

The c command can be used replace some text. It is invoked with a movement command as it's argument. For example, cw will enter Insert mode and replace the current word with the inserted text.

Deleting

The x command will delete the current character. It can be prepended by the number of characters to be deleted. 5x will delete the 5 next characters.

The d command uses a movement command as an argument to decide what do delete. dw will delete the current word. Writing dd will delete the current line. Like the x command, d can be prepended by a number.

See also

References

  • Beginner's tutorial on vi, available in the sources from src/usr.bin/vi/docs/tutorial/vi.beginner (unfortunately not installed by default)
  • Advanced tutorial on vi, available in the sources from src/usr.bin/vi/docs/tutorial/vi.advanced
Retrieved from "http://wiki.netbsd.se/vi"
Personal tools