Vi / Vim: Save And Quit The Editor Command

I am a new vi (vim) text editor user. How do I quit the vi editor and save my changes under a Linux or Unix like operating systems?

vi or vim (Vi IMproved) is text editor or a programmers text editor. You can edit programs or configuration files on the Linux/Unix server. Vim is upwards compatible to Vi. You can start vi/vim by typing the following command:
vim file
vi file
vim [options] filename

How to save and quit the vi or vim text editor

To save and quit the vi or vim editor with saving any changes you have made:

  1. If you are currently in insert or append mode, press Esc key.
  2. Press : (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt.
  3. Enter the following command (type 😡 and press Enter key):




  4. Press ENTER key
  5. This will quit the editor, and all changes you have made to the document will be saved to the file.

Use x to save a file and exit:

Fig.01: Vi / vim save and quit demo.

How to trash all changes in vi/vim

If you do not want to save any changes, first press Esc key. To exit Vim without saving changes press :q! followed by ENTER key.

Getting more help about vim commands

Want to learn vim text editor? Start the vim tutor by typing the following command at the Linux, macOS, and Unix-like system:
$ vimtutor
$ vimtutor it
$ vimtutor fr

Summary of vim/vi commands

Press the ESC key
Type :q!
Press the ENTER key
Exit vim without saving changes i.e. discards any changes you made.
Press the ESC key
Type :wq
Press the ENTER key
Save a file and exit.
Press the ESC key
Type 😡
Press the ENTER key
Save a file and exit.
This entry is 3 of 4 in the Exit From Linux and Unix “app” Tutorial series. Keep reading the rest of the series:
  1. How To Exit From top Command In Linux / Unix / BSD / OS X
  2. How To Exit Vim Text Editor Command
  3. Vi / Vim: Save And Quit The Editor Command
  4. How to reload .vimrc file without restarting vim on Linux/Unix

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...