Vim – save and close buffer in one command

buffervim

I have a bunch of text files in a directory and I know I'll need to edit each one indiviudaly.

I start at the commend line with

vim *.txt

which opens the files as seperate buffers in vim and leaves me looking with 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 recommend way to save and close the buffer you are working on in one felt swoop?

Best Answer

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 :writes 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
Related Question