How to exit Vim

Josh Sherman
4 min read
Software Development Vim / Neovim

I’m sick and fucking tired of being asked if the reason that I use vim (currently using nvim) is because I can’t figure out how to exit.

I get it, you saw some stupid shit on Stack Overflow and you think everybody that uses vim is incapable of knowing how it works.

After 20+ years of using vim as my primary code and text editor, I assure you, it’s not because I don’t know how to exit the damned thing.

In fact, I’d argue that I “exit vim” too regularly.

I’m notorious for exiting out of vim when I want to switch files. I use fzf and know my way around newrw and even used NERDTree in the past, but I’ve struggled to get opening files from within vim as part of my work flow.

Things have improved substantially over the last 2 years from a healthy dose of ridicule from my buddy Collin and my own internal desire to be the most productive individual as I can be.

Enough about me and how great I am at exiting vim, let’s talk about the many ways you can go about properly exit vim and how not to.

How not to exit vi, vim or nvim

CTRL-C while seemingly the “right” solution to abruptly abort a command, is actually the command to switch from INSERT to NORMAL mode.

If you were to hit CTRL-C while in NORMAL mode in vim and nvim, you’ll be greeted with an explanation of how to properly exit the editor. For vi, it’s simply ignored.

Even if it did work, it would cause you to lose any unsaved work depending on if you use swap files or not.

CTRL-Z will throw vim to the background, but not actually quit. You can bring vim back to the foreground by issuing the fg command.

This is my preferred way to get to the command-line while inside of vim and nvim.

My reasoning here, instead of using :term is that I was a long time :sh user. With the removal of :sh in nvim in favor of the more modern :terminal I felt that I should probably just get back to basics.

My use of CTRL-Z is something I can use with any command-line utility I’m using and isn’t specific to vim or nvim. I’m a big fan of ubiquitous commands like that.

How to properly exit vi, vim and nvim

If you are currently in INSERT mode, hit ESC or CTRL-C. This will drop you back to NORMAL mode, where we can issue a command.

The command :q or more verbosely, :quit, will do just that. That’s assuming you have touched all of your open buffers or tabs and haven’t made any changes that still need to be saved.

If you have made changes, or have open buffers or tabs and aren’t concerned with saving any changes, you can force quit, by appending a bang to the quit command: :q!.

BOOM, you exited vim.

If you did make changes and are interested in saving them, you need to also issue the “write” command, which tells vim to save / write the file to disk. Because vim is amazing, you can easily chain the write command with the quit command by issuing :wq.

The same rules apply if you have other open buffers or tabs you haven’t touched, or have unsaved changes. If you aren’t concerned with the other open files, you can slap our good friend bang to the end of the command to save the current buffer and force quit: :wq!.

But wait, there’s more! If you do want to save all of the open buffers before quitting, you can sneak in the a character which tells the write command to write all of the files: :wqa.

To me, the command reads like “write and quit all” even though it’s actually “writing all then quitting”.

Let’s say one of the files you have open is read-only. We can sneak in the bang again to force the writing of the file: :wqa!. This works great if the files are read-only and you have permission to write to them.

If you have files open that you don’t have permission to write to, you’re going to have to either update the permissions of the file(s) or exit (which you now know how to do!) and try again as after switching to a user that has the correct permissions.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles