| CIRCA Unix
Services
This handout can be found online at: | Using the vi editor August 23, 2005 |
The vi editor is a standard text editor that's available on all Unix systems. Vi is a low level editor for experts.This document is intended for computer users with some prior experience with Unix or other systems. You should understand the purpose of a computer editor. If you need an introduction to Unix and Unix editors, refer to texts describing the Unix system.
The other side of this handout is a brief reference to the common vi commands. There are many other commands and features of vi. On most Unix systems, you can view the online manual pages for vi by typing this command:
man vi
To use the vi editor, you'll have to know how to use the control key (Ctrl) and the escape key (Esc).Use the control key (Ctrl) in combination with another key to send a special control character to the computer. The control key is like a shift key that is held down while another key is pressed. For example, to type Ctrl-U, press and hold the the Ctrl key and press the U key.
The Esc key sends a special character, called the escape character to the computer. If your terminal or workstation does not have an Esc key, or if the Esc key doesn't seem to work correctly, you can type Ctrl-[ to send the escape character.
The vi editor operates in two modes: command mode and text input mode. You are in command mode when you start the editor. You must use one the commands listed under "Commands to enter input mode" on the other side of this handout.In command mode, you press one or more keys to delete text or move the cursor on the screen. As is the case in Unix, commands are case-sensitive. Be sure to enter the commands in either upper or lower case as required.
Most commands are entered without pressing the Return key. A few commands do require the Return key and this is noted on the other side.
When you're in text input mode, everything you type is inserted into the file. When you're in text input mode, you can't use commands or the arrow keys to move the cursor. You can only enter text. To leave text input mode and go back to command mode, press Esc.
To start the vi editor, type the vi command with the name of the file to be edited. For example, to edit the file test.f, type this command:vi test.f
The vi editor will read a copy of the file into the editor's memory and any changes will be made to the copy. You must make sure the revised file is written back to the original file before leaving the editor.
To save your file and leave the editor, do the following:
- Press Esc to make sure you're in command mode.
- Type ZZ to write out the modified file and leave the editor.
You might want to leave the editor without writing your changes to the file. If you want to leave the editor without making changes to the original file, do the following:
- Press Esc to make sure you're in command mode.
- Type :q! and then press the Return key.
To go back to command mode, press Esc.
Command Where text is inserted
------- ----------------------------
i Before the cursor
a After the cursor
A At the end of the line
o In a new line below the line
the cursor is on
O In a new line above the line
the cursor is on
If your terminal or workstation has arrow keys, you may be able to use these keys to move the cursor left, right, up, and down.Command How the cursor moves ------- ---------------------------- h Left one character l Right one character j Down one line k Up one line b Backward to start of word e To the end of word 0 Backward to start of line $ To the end of line Ctrl-d Down half a screen Ctrl-u Up half a screen Ctrl-f Forward a full screen Ctrl-b Backward a full screen nG To the nth line in the file G To the last line in the file
Command What's deleted ------- ----------------------------------------------- x Character at the cursor position nx n characters starting at the cursor dw The word the cursor on ndw n words starting with the word the cursor is on dd The line the cursor is on ndd n lines starting with the line the cursor is on
Search commands are used to find a pattern of characters in the file and move the cursor to the location of that pattern.
Command Search how -------- -------------------- /pattern Forward for pattern ?pattern Backward for pattern n Repeat last searchYou need to press Return after /pattern or ?pattern.
After commands beginning with a colon (:), press the Return key.Command What it does ------- -------------------------------------- :w Writes altered text to original file :wfilename Writes altered text to filename :q Quits if no unsaved changes :wq Writes changes and quits ZZ Writes changes and quits :q! Quits without saving changes Ctrl-g Shows number of current line u "Undoes" last deletion U "Undoes" all changes to current line