An introduction to the vi editor (2024)

Table of Contents
Vi edit modes Vi shortcuts

Posted: August 20, 2019 | | by Ken Hess (Sudoer alumni)

Gray at the temples and in need of reading glasses, the middle-aged Vi (pronouncedvee-eye) editor, released in 1976, is still a system administrator's best friend.

This advanced, yet straightforwardcommand-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 edit source code.

Vi is often a symbolic link to Vim (Vi Improved) or an alias to Vim, an enhanced version of Vi.

[ Get theVim cheat sheet. ]

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

$ vi filename.txt

Skip to bottom of list

Vi edit modes

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 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.

[ Get more out of your text editor: 5 Vim features for power users. ]

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 Escto 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. In Vi, write means save, and quit means exit. If you’ve made mistakes along the way while editing and want to back out (abandon) all non-saved changes, enter Command mode by pressing Esc and typing:q! This command quits without saving any changes and exits Vi.

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.

Skip to the bottom of list

Vi shortcuts

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 list 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.

  • $ 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.

[ Want to switch your text editor? Download theEmacs cheat sheet. ]

An introduction to the vi editor (2024)
Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 5905

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.