How do I exit vi editor after saving?

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.

Posted: August 20, 2019 |%t min read| by Ken Hess (Sudoer alumni, Red Hat)

Image

How do I exit vi editor after saving?

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

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

Image

How do I exit vi editor after saving?

Download now

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 edit source code. 

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

[ Get the Vim cheat sheet. ]

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

$ vi filename.txt

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.

[ Looking for a different text editor? Download the Emacs cheat sheet. ]

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

More Linux resources

  • Advanced Linux commands cheat sheet
  • Download RHEL 9 at no charge through the Red Hat Developer program
  • A guide to installing applications on Linux
  • Linux system administration skills assessment

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

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 — 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.
  • vi 2 — Yank (copy) a line of text.
  • vi 3 — Paste a line of yanked text below the current line.
  • vi 4 — Open a new line under the current line.
  • vi 5 — Open a new line above the current line.
  • vi 6 — Append to the end of the line.
  • vi 7 — Append after the cursor’s current position.
  • vi 8 — Insert text at the beginning of the current line.
  • vi 9 — Go to the beginning of the word.
  • i0 — Go to the end of the word.
  • i1 — Delete a single character.
  • i2 — Delete an entire line.
  • i3 — Delete X number of lines.
  • i4 — Yank X number of lines.
  • i5 — Go to the last line in a file.
  • i6 — Go to line X in a file.
  • i7 — Go to the first line in a file.
  • i8 — Display the current line’s line number.
  • i9 — Move left one character.
  • :wq0 — Move down one line.
  • :wq1 — Move up one line.
  • :wq2 — Move right one character.

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

Image

How do I exit vi editor after saving?

Linux skills: 9 tutorials to get more from your text editor

Are you getting everything you need out of your text editor? Read Enable Sysadmin's recent articles about Linux text editors to find out what you're missing.

Posted: July 22, 2022

Author: Vicki Walker (Editorial Team, Red Hat)

Image

How do I exit vi editor after saving?

How to get started with the Vi editor

Once you've committed Vi's keyboard shortcuts to muscle memory, watch how fast you work.

Posted: January 25, 2022

Author: Seth Kenlon (Editorial Team, Red Hat)

Image

How do I exit vi editor after saving?

Top five Vim plugins for sysadmins

My list of the top five Vim plugins—plus a bonus.

Posted: October 16, 2020

Author: Ricardo Gerardi (Sudoer, Red Hat)

Topics:   Command line utilities   Text editors   Linux  

How do I exit vi editor after saving?

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

Red Hat Summit 2022: On Demand

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

Register for free

Image

How do I exit vi editor after saving?

Learn how Unix influenced Linux, understand Ansible lists and dictionaries, and more tips for sysadmin

Check out Enable Sysadmin's top 10 articles from November 2022.

Posted: December 5, 2022

Author: Vicki Walker (Editorial Team, Red Hat)

Image

How do I exit vi editor after saving?

6 ways to get information about your CPU on Linux

Getting insight into what CPU you're running is a useful trick to know.

Posted: December 5, 2022

Author: Seth Kenlon (Editorial Team, Red Hat)

Image

How do I exit vi editor after saving?

How to use the lsof command to troubleshoot Linux

The Linux lsof command does more than list open files; you can also use it to diagnose potential bottlenecks.