Vim: Do you Know How to Exit Vim? You Probably Know, and Probably Don’t Know All.

Bruce Wen
Code.Art
Published in
5 min readJul 31, 2023

--

If you are a Vim fan, you probably know about :q , :wq, :qa!and as such. Meanwhile, you probably don’t know more than that, do you?

Preface

All the commands below are used to exit Vim, but they are not all the commands to exit Vim.

:q, :conf q, :q!, :cq, :2cq!, :cq! 2, :wq, :wq!, :wq /tmp/tmp.NH31JOv96N, 
:wq ++ff=dos /tmp/tmp.NH31JOv96N,
:10wq! ++enc=laten1 init.el,
:x, :x!, :xi, :xit, :exit, :exi, :exi!
ZZ, ZQ
:qa, :qa!, :conf qa, :quita, :wqa, :wqa!, :xa, :conf wqa, :conf xa

So, why does Vim have so many different commands just for exiting?

It’s decided by user or use cases. In reality, the usage of an editor is always too complex to tell. Different users in different situations have different requirements. Developers of Vim want to or have to satisfy those requirements. Therefore, it results in complexity and a steep learning curve. Luckily, Vim has sufficient and easy-to-use documentation.

Do we need all of the different commands for Vim exiting?

As an individual, we only need a few of them. That’s why most Vim fans only even know a few of the commands above. That’s not something to be ashamed of. Almost everyone.

What typical use cases caused so many different commands to exit Vim?

Here are some examples:

  • Only one file is opened, and no modification
  • Only one file is opened, has a modification, not saved yet
  • Only one file is opened, has a modification, saved
  • Multiple files are opened, and no tabs, no files are modified
  • Multiple files are opened, and no tabs, several files are modified and not saved
  • Multiple files are opened, and in different tabs, no files are modified
  • Multiple files are opened, and in different tabs, several files are modified and not saved
  • ……
  • I want to get confirmation before I exit Vim.
  • I want to save the current buffer to another file.
  • I want to save the current buffer to another file with a different encoding.
  • I want to save the current buffer to another file with a different line feed.
  • I want to save one part of the current buffer to a new file.
  • ……

This list could be very long.

Now, we can say that developing generic software is too complex to be done by one person. Nobody has enough energy to handle no-ending requirements from no-ending users. Many open-source projects are finally abundant. Among them, there are many excellent works.

Fortunately, as long as the software can bring value to the users, there will always be people who take these projects or create new ones based on them.

Do we need to know all commands to exit Vim? Not. Not all use cases we might encounter. We only need to know how to do it when we need to do it. For most of us, we don’t need to be Vim experts. We only use it for our own business. It’s a tool, an editor. Isn’t it?

So, what’s the purpose of this article?

It’s to share some useful use cases about exiting Vim for you who might need them. No matter now or in the future. So, try to keep reading.

Use Cases

A: I already start to edit the git commit with Vim, but I don’t want to continue this git commit, what shall I do?

Believe me. This is a very typical use case.

Trust me, most developers (including me before my research for this article) don’t know how to handle this situation. And finally, they have to kill the process where the git commit was started.

Note: if type :q! then the commit will continue.

So, how to quit Vim and stop the git commit process gracefully?

Use the command :cq!2 which is to quit Vim with error code 2 which will cause git to abort the commit process.

After that, you will see below messages from git:

hint: Waiting for your editor to close the file... error: There was a problem with the editor 'vim'.
Please supply the message using either -m or -F option.

B: I have many opened files. Some of them are read-only. I want to save all modified files and exit Vim.

At least, we have 2 different ways to do so: :wqa! and :xa! .

What will happen if we use :wqa and :xa which have no the ending ! ? Simply, Vim won’t exit if any changes are made to read-only files.

The error message below might be printed when :wqa or :xa is used when read-only files were changed.

E45: 'readonly' option is set (add ! to override)

C: I want to automatically remove all files without a file name when I quit Vim.

In use case B, if any buffers have no filename, then an error message will be printed and the exit process is blocked.

📓 This only happens when the buffer without a file name has modifications.

E141: No file name for buffer 1

If we don’t care about those files without a filename, then how can we exit Vim without the error?

Actually, this is not related to exiting Vim but to how to handle a new buffer without saving to disk with a file path. After handling the new buffers not saving with a file path, then do exit Vim. If we want to combine two steps into one, we need to define the function.

" Function to check if a buffer is unnamed
function! IsBufferUnnamed(bufnr)
return empty(bufname(a:bufnr))
endfunction

" Function to remove unnamed buffers from the buffer list
" if they are in the buffer list
function! RemoveUnnamedBuffers()
let buf_list = filter(range(1, bufnr('$')), 'IsBufferUnnamed(v:val)')

for bufnr in buf_list
if buflisted(bufnr)
silent execute 'bwipeout! ' . bufnr
endif
endfor
endfunction

" Remove unnamed buffers, forcely save other buffers, and then exit Vim
function! RemoveUnnamedBuffersAndExit()
call RemoveUnnamedBuffers()
execute 'xa!'
endfunction

" Map to ,vv
:nnoremap ,vv :call RemoveUnnamedBuffersAndExit()<CR>

--

--