The vi editor (visual editor) is one of the oldest and more popular text editors on computers running the Unix Operating System (OS). The vi editor can be used from any computer terminal interfacing with a computer running Unix, since the editor relies on the standard alphabet keys for specific commands. Since vi is not a WYSIWYG (What You See is What You Get) text editor, it does not provide the formatting capabilities word processors do for paragraph indentations, font, and line centering. New vi users need to learn the following: opening and closing a file, file editing, and moving around in a text file.

How to Start vi

Step 1 – Open an existing file by entering the following command at the terminal command line, then press the “return” key” vi myFileName

If the file is not in the current directory, the fully qualified file path must also be included.

Step 2 – Create a new file by entering the following command:

vi myNewFile

Enter Text in vi

When vi is first started, it loads in command board with the keyboard keys doing text deletion and cursor movement. In order to enter text, enter the “insert mode” by typing the letter “i”. No visual change will occur on the screen, but text can now be entered. Press “return” at the end of each line of text. The vi editor will not wrap text automatically at the end of a line on the screen. Instead, it will automatically break the line of text in mid word if the “return” key is not pressed prior to the end of line.

How to Move the Cursor in vi

One of the initial tasks that new users of the vi editor find challenging is moving the cursor to a new position in the document. In order to do so, place the vi editor in command mode by pressing the “Esc” key. If unsure about what mode the editor is in, press the key until a beep is heard. The following letters can move the cursor:

l – moves the cursor right one space

k – moves the cursor up one line

j – moves the cursor down one line

h – move the cursor left one space

Once the cursor is moved as far as possible in a given direction, the terminal will beep.

How to Edit a File in vi

In order to edit a file in vi, place the editor into command mode. The letters’ meanings depend on whether they are typed in lower or upper case. Some commands can also be repeated a predefined number of times by including a number prior to the command.


Vi Editing Commands

Character deletion – x (with the cursor placed on the incorrect letter)

Delete the character prior to the cursor – X

Delete multiple characters – (number)x (will delete the character highlighted by the cursor and the next three

Delete words – dw (with the cursor on the first letter of the word)

Delete lines – dd (the cursor can be at any location on the line to be deleted)

Delete from the cursor to the end of the line – D

Replace characters – r (with the cursor placed on top of the character to be replaced).

Replace words – cw (with the cursor at the start of the word to be replaced).

Replace lines – C (after typing the command, enter the new text then press the “Esc” key).

Insert text – i (pressed after the cursor is positioned where the next text is to be inserted. The text is inserted prior to the cursor).

Append text – a (with the cursor placed on the final letter of the line to append the text).

Insert a blank line above the current line – O

Undo edits – u

Undo all edits on a line – U

Undoing

To undo your most recent edit, type

u

To undo all the edits on a single line, type

U (uppercase)


Useful Vi Commands to Move Around Files in Command Mode

“Control” “f” Scroll forward one screen in the editor

“Control” “b” Scroll backward one screen in the editor

“Control” “d” Scroll down one-half screen in the editor

“Control” “u” Scroll up one-half screen

w – Move forward one word at a time

b – Move backward a word at a time

$ – Move to the end of the line

0 – Move to the beginning of the line

H – move to the top line of the screen

M – Move to the middle of the screen

L – Move to the last line of the screen

G- Move to the last line of the file

ZZ – Exit and Save a file

:w – Save a file

:q! – Discard file changes and quit