6

I opened vi myfile.txt (or vim myfile.txt, or nvim myfile.txt) to use vi/Vim/NeoVim to edit a file.

Now that it is open, how do I quit?

If I open vim with no file, the intro screen says :type :q<Enter> to exit, but when I type that, it ends up somewhere in the contents of my file or what looks like a new file. How can I use that command?

8
  • 1
    Actually, you should see an answer to your question on opening Vim without an argument. :q is mentioned in the greetings message.
    – Biggybi
    Commented Dec 17, 2021 at 0:46
  • 1
    Welcome to Vi and Vim! Based on your rep on other sites, I'm going to assume good faith and that this is not some kind of troll. But yes, :help quit, or, as the :intro screen tells you, :q, or, as Ctrl-C tells you, :qa, or, there's <your favorite search engine> which unfailingly brings up the post @filbranden linked…
    – D. Ben Knoble
    Commented Dec 17, 2021 at 1:38
  • 2
    @D.BenKnoble - I asked this question in good faith and framed it for casual users because it is something I have to teach teammates on a regular basis. Having this question answered seriously and directly on the Vi.SE site seems helpful to those new users. Would copying the answer from SO and switching this to community wiki be a good solution?
    – Freiheit
    Commented Dec 17, 2021 at 2:06
  • 2
    Since the tag was mentioned, note that the tag itself has information too: vi.stackexchange.com/tags/quit/info
    – muru
    Commented Dec 17, 2021 at 3:13
  • 1
    @Freiheit Yes, I think community wiki is the way to go. Personally, I'd prefer not to copy an answer verbatim from SO, but I guess adapting from them and keeping a reference would be a way to go... Do you mind if I convert the question itself to a wiki as well and edit it somewhat?
    – filbranden
    Commented Dec 17, 2021 at 4:03

1 Answer 1

6

In short, hit the Esc key to make sure you're in "Normal" mode, then type :q (which will appear on the last line of the screen) and press the Enter.

If Vim complains that you have unsaved changes, you can use :q! Enter to discard changes and exit, or you can use :wq Enter to write your changes and exit.


vi/Vim/NeoVim is a modal editor, so you need to be in "Normal" mode to be able to enter commands in it. In almost all cases, you can just hit the Esc key to enter Normal mode.

At this point, you can use one of the "Ex" commands, which are those that start with :, to exit Vim. Once you press the : key, you will enter "Command-line" mode, where you'll see a : at the beginning of the last line of your terminal or window where vi/Vim/NeoVim is running. You can then type a command, which will show up on that line, and finally press Enter to execute it.

You can use one of the following commands to quit Vim:

  • :q or :quit to quit. It works when there's a single file being edited (no splits) and it's not modified.
  • :q! or :quit! to quit without saving the current file.
  • :wq to write (save) the current file and quit. You can also use :wq! when the file is marked as read-only (though not always vi/Vim/NeoVim will be able to save it in that case.)
  • :x or :exit or :exit to write, if file was modified, and quit. This similar to :wq, but only try to save if there are changes to the file.

These commands work when there's a single file being edited or viewed. If you have splits (which Vim calls "windows") with multiple files opened at the same time, the commands above will quit (or save and quit) a single window at a time.

If you have multiple files open, you might want to use one of the following:

  • :qa or :quitall or :qall to quit all windows. This is roughly equivalent to using :q repeatedly in each one of them.
  • :qa! or :quitall! or :wall! to quit all windows discarding changes. This is akin to using :q! on each window.
  • :wqa, :wqall, :xa or :xall to save and quit all windows, similar to :wq (or :x) does for a single window.
  • :wqa! or :wqall! to force save and quit all windows, same as :wq! does.

There are also two Normal mode commands you can use to quit. Note that these are typed directly, without using : first to enter "Command-line" mode, and without an Enter at the end.

The two Normal mode commands you can use to quit are:

  • ZZ to save and quit, same as :x.
  • ZQ to quit, discarding changes, same as :q!.

In case there are multiple windows ("splits"), these commands will only close a single split, you might need to repeat them to close others if you wish to use them in that case. Note also that these need to be typed uppercase. ZZ and zz are different commands.

Vim/NeoVim has an excellent help system, that you can access with the :help command (again, first hit Esc to ensure you're in "Normal" mode, then : to enter "Command-line" mode, then type help and press Enter.) You can find more answers there, together with instructions on how to use the help system, and a tutorial for beginners.


(Much of this question and answer was inspired by ”How do I exit the Vim editor?”, one of the most upvoted questions in Stack Overflow.)

2
  • Sorry @NoahBroyles, I made this question into a community wiki and that affected your answer. I'm planning to import some snippets from the popular S.O. answers, so a wiki is appropriate here. Again, sorry for impacting your answer here.
    – filbranden
    Commented Dec 18, 2021 at 18:16
  • 1
    No problem, just seized an easy way to earn a couple points for a question I knew the answer too lol Commented Dec 20, 2021 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.