31

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?

Aulis Ronkainen
  • 2,612
  • 42
  • 29
  • 27
Joe
  • 3,430
  • 5
  • 30
  • 45
  • My recomended way would be to just move to the `:next` buffer and write/close everything once your task is finished with `:wqa`. This supposes that you have `:set hidden` in your `~/.vimrc`. – romainl Jan 26 '13 at 13:14
  • @romainl: not as useful, because if you open 30 buffers for editing, you have to keep track of how many you've cycled through. – naught101 Jul 04 '21 at 03:58
  • @naught101, I'm not sure why you think that. `:wqa` will write and quit every argument in the argument list but with a single command versus 30. – romainl Jul 04 '21 at 08:25
  • Ah, I see. Was confused, because I'm used to using `:bnext`, and that loops after the last open buffer. Yes, `:n` is good. – naught101 Jul 05 '21 at 03:41

3 Answers3

31

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
Ingo Karkat
  • 22,638
  • 2
  • 45
  • 58
  • 8
    You've mentioned two methods of achieving what the OP requested, but the second one isn't obvious: `:w|bd`. – Jonno Aug 15 '16 at 05:50
  • --> This has the advantage of fitting the approach the OP uses, although I'd agree `:next` makes more sense in some cases. – Jonno Aug 15 '16 at 05:51
5

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).

apostl3pol
  • 170
  • 1
  • 4
  • 2
    The built-in `ZZ` comes to mind, but that ends up saving and quitting Vim entirely, rather than just deleting the buffer. Lowercase `zz` might be an interesting alternative mapping, for anyone who finds macros more valuable than centering the view on the current cursor line. – 8bittree Oct 17 '16 at 20:00
  • 1
    I recently changed it to `Q`, after realizing just how valuable recording in Vim can be. – apostl3pol Apr 13 '17 at 03:06
2

I use abbreviation instead of a command

cnorea wd w\|bd

So I can see what it is actually doing when I hit space after typing :wd, or if I want to be fast I just press enter, it still does the conversion.

isacnewton
  • 21
  • 3