Skip to main content

An introduction to the vi editor

The lowly and now middle-aged vi editor is still a favorite among *nix system administrators and remains an essential tool for command-line file creation and editing.
vi vs. emacs

"VI vs. Emacs" by mrbill is licensed under CC BY 2.0 

Gray at the temples and in need of reading glasses, the 43-year-old vi (pronounced vee-eye) editor is still a system administrator’s best friend. This advanced, yet simple to use, command line editing program shows no sign of slowing down. You can use it to edit configuration files, create a grocery list, write a letter home to ask for money, create a new script, or even to edit source code. 

[ Download now: Vim cheat sheet ]

Note: vi is often a symbolic link to vim (vi Improved) or an alias to vim.

It’s easy to invoke vi. At the command line, you type vi <filename> to either create a new file, or to edit an existing one.

$ vi filename.txt

The vi editor has two modes: Command and Insert. When you first open a file with vi, you are in Command mode. Command mode means that you can use keyboard keys to navigate, delete, copy, paste, and do a number of other tasks—except entering text. To enter Insert mode, press i. In Insert mode, you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate text, and use vi as a free-form text editor. To return to Command mode, press the Esc key once.

Note: In vi's Command mode, almost every letter on the keyboard has a function.

To save a file, you must first be in Command mode. Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit. To the non-vi initiated, write means save, and quit means exit vi. If you’ve made mistakes along the way in your editing and want to back out (abandon) all non-saved changes, enter Command mode by pressing Esc and type :q! This command quits without saving any changes and exits vi.

The best way to learn vi is to create a new file and try it out for yourself. Feel free to use the common keyboard shortcut table below to help you learn vi’s extensive vocabulary. This list of shortcuts is by no means exhaustive, but they will enable you to edit files and learn vi in a short amount of time.

Note: Always make a copy of an existing file prior to editing with vi or any editor. This is especially critical when editing system and configuration files.

Command Purpose
$ vi <filename> Open or edit a file.
i Switch to Insert mode.
Esc Switch to Command mode.
:w Save and continue editing.
:wq or ZZ Save and quit/exit vi.
:q! Quit vi and do not save changes.
yy Yank (copy a line of text).
p Paste a line of yanked text below the current line.
o Open a new line under the current line.
O Open a new line above the current line.
A Append to the end of the line.
a Append after the cursor's current position.
I Insert text at the beginning of the current line.
b Go to the beginning of the word.
e Go to the end of the word.
x Delete a single character.
dd Delete an entire line.
Xdd Delete X number of lines.
Xyy Yank X number of lines.
G Go to the last line in a file.
XG Go to line X in a file.
gg Go to the first line in a file.
:num Display the current line's line number.
h Move left one character.
j Move down one line.
k Move up one line.
l Move right one character.


[ Download now: Vim cheat sheet ]

Topics:   Command line utilities   Text editors   Linux  
Author’s photo

Ken Hess

Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, all available on demand.

Related Content