Vim - save and close buffer in one command
I have a bunch of text files in a directory and I know I'll need to edit each one individually.
I start at the command line with:
vim *.txt
Which opens the files as separate buffers in Vim and leaves me looking at the first one. I edit it, then I use ':w' to save it and ':bd' to close the buffer and move on to the next one.
This ':w:bd' to save and close the buffer feels long to me, and I suspect there's a more Vim ninja way of doing it. What's the recommended way to save and close the buffer you are working on in one felt swoop?
Solution 1:
When passing the files to Vim on the command-line, they are not only opened in buffers, but also populate the argument list. Therefore, you can use commands like :next
and :first
to navigate through them (and :argdo
for batch processing, which can be a nifty trick). The command I recommend for your question is :wnext
(short form :wn
), which :write
s the current buffer and then goes to the :next
one.
You don't need to explicitly :bdelete
a buffer, especially not when you're launching Vim from the command-line with a set of files and then quit it when you're done. (The only exceptions I can think of is unloading a huge file to save system memory, or re-using a single GVIM instance for many different edits.)
However, if you really want this, just define a custom command, e.g.
:command Wd write|bdelete
Solution 2:
While I agree that you don't have to delete open buffers you aren't currently using, I like to delete them because I have vim-airline's tab line enabled, which shows all open buffers all the time at the top of the window. I have the following in my .vimrc
:
nnoremap qq :w\|bd<cr>
qq
saves and closes the current buffer, but also messes with the "record macro" functionality of vim (which I'm okay with).