[ Current Issue Home | Issue #4 Home | FAQ ]��

The FreeBSD 'zine
Featured Articles: vi
## A Tutorial
## Rick Patel <>

Vi is supposed to be an acronym for VIsual editor. It was one of the first screen oriented editors developed on UNIX system, and since then there have been many flavors of Vi developed such as Elvis, Vim, and Nvi. This article was written as an introduction and tutorial for doing basic editing with Vi. The commands listed in this article should work with all the flavors of Vi.

Invoking and Quitting Vi

You can invoke vi many different ways depending on what you want to do. If you want just want to view file, invoke vi with a -R switch for read-only.

	$ vi -R /etc/rc.conf

You can get the same result by invoking `view` with the filename. Other useful command line switches are -r, which will recover the specified files if there was an accident while editing the file previously and it was not saved, -e which will drop you into ex editing mode, type vi at the : prompt to return to the vi editor.

Most trouble newbie vi users have is figuring out how to quit. At anytime you can quit indefinitely, abandoning all the changes to current file, by typing :q!. The commands starting with : mean they are ex commands, all the other commands entered during command mode editing are vi commands. Invoke vi by typing vi at the shell prompt. To open and existing file to edit or to create new file type :e <file-name>. If file already exists vi will show you the file contents in the windows and wait for you input or it will create the file.

Moving Around in Vi

Another thing newbie vi users get frustrated about is the movement keys. Vi was created way back for a specific type of terminal which did not have the arrow keys that we have today. While in the command mode, vi uses the keys 'h' for left, 'j' for down, 'k' for up, and 'l' for right. This arrangement of keys could take some time of getting used to, so most of vi installation on modern unices have appropriate keys mapped. After you invoke vi with no file name arguments it presents you screen with bunch of ~ characters. This means that there is no text for those lines and you can not move around in that area. You can move from one place to another many different ways in vi. Here is list of some frequently used movement commands:

	w	move to next word (punctuation separates words)
	W	move to next word (only white space separate words)
	e,E	move to end of word
	0	move to beginning of line
	$	move to end of line
	G	move to end of file (use nG to move to line n)
	H	move to top line of screen
	L	move to bottom line of screen
	+	move to next line
	-	move to previous line
	^	first non-blank letter of current line 

The above commands can be used where a movement is expected as argument to command. For example, use 'dw' to delete next word, 'd0' to delete up to the beginning of the line, 'd$' to delete to end of line, and so on. The following commands can be used to redraw buffer a full screen at a time:

	^F	Scroll forward
	^B	Scroll backward
	^E	Scroll window forward (by default 1, use [count] ^E)
	^D	Scroll down half screen
	^U	Scroll up half screen
	^L	Redraw screen

Other commands that are helpful are :n or nG which is ex command to move to line n, <ctrl-G> to display line number and total lines of buffer, and G to move to last line in buffer.

Basic Editing

Vi has two modes of operation: command mode and input mode. Command mode is when lets you input commands like as 'l' for left movement. Input mode is when vi lets you enter text in the buffer. You can switch from input mode to command mode anything by pressing the ESC key. When in command mode, the commands that begin with : are ex editor commands. There are numerous editing commands you can use to insert text in vi. The most basic ones are 'a' and 'i'. Notice these are not ex commands. To append text after the cursor type 'a' at the command mode. Type 'i' at the command mode to start inserting text at the cursor. You can return to command mode by hitting the ESC key. If you want to see what mode you are in at any time, type ':set showmode' at the command prompt which will show you either "Insert" or "Command" in the lower right corner. Use the 'd' command to delete words. For example, to delete next four words starting at cursor, type 'd4w' in vi command mode. Here, 'd' stands for delete and '4w' tells vi you want change to affect four words. The basic usage of 'd' command is d[count]<movement>. Here count is optional. Movement command can be any command you use to move around in vi. Here are list of frequently used editing commands:

	a		append after cursor
	A		append to end of line
	i		insert before cursor
	I		insert at beginning of line
	<ESC>		return to command mode
	[count]cw	change word
	[count]cc	change line
	C		change to eol
	[count]dd	delete current line
	[count]x	delete character at cursor
	[count]X	delete character before cursor
	d[movement]	delete until movement command end  
	[count]s	substitute character
	u		undo last change
	U		restore current line

The command :w writes the buffer to file. If you opened the file in read-only mode you can invoke :w!, which forces write. You can force command to be executed without warning from vi with a '!'. If at any time during editing you decide to revert the changes made to file and reload the file in buffer, use the :e! command. Vi can work with multiple files at same time. For instance, if you invoked vi from command line as:

	$ vi main.c proc.c

vi will bring up the main.c file buffer and wait for your command. You can use the :n command to go to the next file. If you decide you want to go back to file main.c, use :e# command which will move you back to last file buffer. The :e# command can only be used to move between two files at time. % symbol in ex commands denote current filename, and # stands for alternate filename.

Copy and Paste

There is way in Vi to hold contents of lines into buffer which can later be pasted. The command 'yy' yanks the current line in default buffer. The 'p' command is used to paste contents of the default buffer. The default buffer has contents of last deleted or yanked text. Here are the yank and delete (or copy and cut) commands:

	[count]"xyy		yank current line into buffer x
	[count]"xd[movement]	cut into buffer x
	[count]"Xd		cut and append into buffer x
	[count]"xp		put contents of buffer x
	[count]ye		yank word

The 'x' here can be a single alpha-numeric character. That is, "1yy would copy the contents of current line into buffer 1.

Search and Replace

There are two basic types of search command: / and ?. When you type / in command mode, vi will wait for text to search then moves forward looking for text. ? works the same way, but vi moves backward for search. Text entered in can be in the form of a regular expression supported by vi. Regular expression is powerful way of pattern matching. For example, /[uN]nix matches either unix or Unix. Use the man page for vi for more information on regular expressions. You can repeat the previous search with 'n', use 'N' to search in opposite direction of previous search. The global command of ex can be used to execute commands on lines containing certain patterns. Usage is:


So to substitute all the lines with words windows to windoze, do:


Macros and Abbreviation

Abbreviations are pretty useful. Here is the syntax:

	:ab [abbreviation] [replacement]
	:unab [abbreviation]

Basically you can tell vi that any time it finds abbreviation in input mode it will replace it with 'replacement'. For example, do:

	:ab unix UNIX(tm)

Now whenever you enter the word 'unix' in text, vi will convert it to 'UNIX(tm)'. Use ':unab unix' to make that go away. The :ab command with no arguments shows all abbreviations defined. Macros in vi map particular keystrokes to a sequence of commands. Macros can be used while you are in input mode, or in command mode. Here is the basic syntax:

	:map key sequence	command mode mapping
	:map! key sequence	input mode mapping
	:unmap key		unmap command mode mapping
	:unmap! key		unmap insert mode mapping

The key from 'map' command only works when in command mode, and key from 'map!' only works in input mode. If you use :map or map! without any arguments it shows all mapped keys for that mode.

More information

Since vi is the most popular editor out there for UNIX, it is documented very well. There are tons of references and also most UNIX books cover vi. For a complete reference on Vi, look at the files in /usr/share/doc/usd/12.vi/* and /usr/share/doc/usd/13.viref/* on your FreeBSD system. Also the man page for vi(1) will give all command line options and summary of commands.

- Rick

Return to Issue #4

Contact: <>
Last modified: $Date: 1999/06/26 05:33:26 $
Copyright � 2023, The FreeBSD 'zine
All rights reserved.