How To: Vi / Vim Save And Quit The Editor Command

See all VI / Vim text editor related FAQs/HowTos
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? Can you tell me command to save and quit file in vim?

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. This page explains how to save a file in Vim or vi and quit the editor under Linux or Unix-like systems.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Text Editor
OS compatibility BSD Linux macOS Unix WSL
Est. reading time 4 minutes

Vim Save And Quit The Editor Command

The procedure to save a file in vim / vi and quit the editor is as follows:

  1. Open the terminal application in Linux or Unix
  2. Next, open a file in vim / vi, type: vim filename
  3. To save a file in Vim / vi, press Esc key, type :w and hit Enter key
  4. One can save a file and quit vim / Vi by pressing Esc key, type :x and hit Enter key

Let us see all commands and examples in details.

How to open a file in Vim / Vi

You can start vi/vim by typing the following command:
$ vim file
$ vi file
$ vim [options] filename
$ vim helloworld.sh

Understanding vim modes

When we start vim/vi text editor, we are in normal mode. This mode allows use to use vim commands and navigate around the file as per our needs. In order to edit text, you need to enter the insert mode by pressing i key. This way, we can edit our program or configuration files such as /etc/nginx/nginx.conf.

Of course, you can go back to normal mode by pressing Esc key again.

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 :x and press Enter key):
    :x

    OR

    :x!

    OR

    :wq!
  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.

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, *BSD, and Unix-like system. For example:
$ vimtutor
$ vimtutor {language}
$ vimtutor it
$ vimtutor fr

How to Save a File in Vim or Vi and Quit the Editor

vimtutor in action

The keyword it or fr is for Italian and French languages. It is the two-letter name of a language. Of course, you can read local documentation about vim text editor using the man command or help command. For instance:
$ man vim
$ man vi
$ vim --help

Summary of vim/vi commands

Command Description
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 :x
Press the ENTER key
Save a file and exit.

Are “vi” and “vim” text editor commands different?

Vi and Vim are two different text editors. Vi is an older text editor created in the 1970s, while Vim is a newer editor that builds on Vi with additional features and improvements. Bill Joy is the original creator of the vi editor. Tim Thompson is the original creator of vim. Here are some of the key differences between vi and vim text editors:

  • Vim offers a wider range of features than vi, including syntax highlighting, code folding, and the ability to use multiple windows and tabs.
  • Additionally, Vim can be customized to a greater extent through plugins and configuration files.
  • In terms of efficiency, Vim is generally faster than vi, particularly when dealing with large files, thanks to its optimized text editing engine.
  • Choose vi for simplicity, or vim for flexibility and customization, when selecting a text editor. On most moder Linux distro vi is just symbolic link to vim.

Conclusion

You learned how to save a file in vim and exit the text editor using various vim commands. See the vim site and the traditional vi site here, or the following pages for more information.

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

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

Vivek Gite is an expert IT Consultant with over 25 years of experience, specializing in Linux and open source solutions. He writes about Linux, macOS, Unix, IT, programming, infosec, and open source. Follow his work via RSS feed or email newsletter.

27 comments… add one
  • Lalu May 12, 2004 @ 1:43

    Got stuck in vim, you saved my day on Red Hat Linux.

    • Izzy May 29, 2017 @ 23:01

      vim put me in jail and the year is 2017.

    • Carl Feb 2, 2022 @ 9:48

      it is 2022 and I’m stuck on RHEL 8 while following an Apache server tutorial.

  • Nitu Nov 23, 2007 @ 13:44

    I was wondering how to get out of this hell and lo, it was pressing escape and typing :q!.

    Hah.

    • Anonymous Sep 2, 2018 @ 11:19

      Yeah, the vim editor put me in jail too, and the year is 2018. Help me, Mr. Nix Kenobi. You are my only hope.

  • aref ghobadi Aug 16, 2015 @ 12:25

    Hi,
    thanks

    w!= save without exiting
    q!= exit without saving

  • Mohammad Jun 14, 2016 @ 13:42

    To save and quit:
    If you are currently in insert or append mode, press Esc key.
    Press: (semi-colon) The cursor should reappear at the lower left corner of the screen beside a colon prompt.
    Enter the following command (type wq! and press Enter key).
    :)

    • Mohammad Jun 14, 2016 @ 13:56

      Press: (colon) The cursor should reappear at the lower left corner of the screen beside a colon prompt.
      colon is right.

  • aaaaaaa Sep 28, 2016 @ 20:15

    none of these commands are working

    • Luke Dec 6, 2016 @ 23:04

      Hit the ‘esc’ key and it will take you out of the cursor mode and give you a cursor at the lower left hand of the screen. Now enter wq! and it will save and quit.

  • Sean Dec 24, 2016 @ 6:22

    Worked for me (Ubuntu 16.4). Thanks

  • Olga Jan 24, 2017 @ 19:16

    Thanks for sharing knowledge!
    Worked just perfectly!

  • Peter Jan 27, 2017 @ 4:35

    worked for me too on Ubuntu. thank you!

  • me Jun 17, 2017 @ 14:18

    The theme become so popular in the entire Www, so I’ve created fun page

    • John Sep 14, 2017 @ 9:00

      Can’t believe I clicked your link :)

  • Amit Shirasao Jul 18, 2020 @ 18:58

    Thx. It helped me.

  • sara Sep 12, 2020 @ 18:55

    thanks so much

  • Anonymous Sep 22, 2020 @ 20:36

    no work
    :wq
    :wq!
    wq!

  • Anthony Dec 9, 2020 @ 21:10

    Stop using :wq! to save and exit!
    :wq is save and quit, the ! tells it to ignore errors and exit. This could potentially leave you with exiting a file unsaved due to an error.

    :q! is exit without saving
    :wq is “save and exit”

    • Shafali Verma Feb 15, 2022 @ 22:22

      Good to know. thank you <3

  • Yvette Hawkins Jun 3, 2021 @ 22:31

    I have work peacefully on my Mac Mini as I started vi by accident while reading uni docs. Hehe.. and then I was like, WTF? How do I get out of this thingy? So Google sent me here, and you saved me like God showing mercy on the poor soul.

  • Anonymous May 2, 2022 @ 14:48

    I am learning vim for college credit. May I suggest full tutorial about vim?

  • Simba Apr 6, 2023 @ 16:40

    Press the Esc key.
    Press : (colon) to open the command prompt n the bottom left corner of the vim window.
    Type x after the colon and hit the Enter. This will save the changes and exit.

  • Charlene Kelly Jun 24, 2023 @ 12:42

    Initially I was searching for instructions on how to exit vim, but I ended up learning some historical background information. I discovered that “vi” and “vim” are two distinct applications. Ultimately, the best text editor for me depends on my needs and preferences. I started learning vim for Debian server and my daily text editor is PyCharm :)

    • Bindu Reddy Jan 9, 2024 @ 8:09

      Charlene, That is so nice to share all information. Thank you.

  • VT Sep 23, 2024 @ 14:36

    Excellence and very useful for first time user. thank u!

Leave a Reply

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.