565

I open several files in Vim by, for example, running

vim a/*.php

which opens 23 files.

I then make my edit and run the following twice

:q

which closes all my buffers.

How can you close only one buffer in Vim?

fakeleft
  • 2,830
  • 2
  • 30
  • 32
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 11
    Note that you can open the files in separate Vim windows using `vim -o a/*.php` (or `-O` to use vertical windows) and avoid the buffer navigation - this is a great method for only a few files, but with 23 files they'll only have a few lines/columns each. – Cascabel Aug 13 '09 at 02:06
  • 1
    @Jefromi: Thank you for pointing that out! I have never use the option -o before. – Léo Léopold Hertz 준영 Aug 13 '09 at 02:32
  • 1
    On the subject of window splits. If you are already in Vim and want to open another file in a split :esp for horizontal :evsp for vertical. – Lanzafame Dec 08 '14 at 04:07

10 Answers10

745

More from manuals:

:bd

Unload buffer [N] (default: current buffer) and delete it from the buffer list. If the buffer was changed, this fails, unless when [!] is specified, in which case changes are lost. The file remains unaffected.

If you know what you’re doing, you can also use :bw

:bw

Like |:bdelete|, but really delete the buffer.

A word of caution: “the w in bw does not stand for write but for wipeout!”

Romain Vincent
  • 2,875
  • 2
  • 22
  • 29
hhh
  • 50,788
  • 62
  • 179
  • 282
  • 4
    Also the possibility of doing ':M,Nbd' to close buffer numbers M to N. Or ':bd N1 N2...' where N# is a buffer number – snowbound Apr 08 '14 at 12:48
  • 114
    The manual says to only use `:bw` if "you know what you're doing", which I don't, so I guess I'll use `:bd`. – jberryman Jun 23 '14 at 19:50
  • 2
    I was confused by this at first - I was opening a new tab to close buffers... but closing the buffer in the tab (using a straight `:bd`) also closes the tab. What you probably want is `:bd n .. [m]` or `:n,mbd` for specific numbers or range of numbers to close, which you can do without looking at the buffers – JonnyRaa Mar 19 '15 at 11:07
  • 6
    `w` is short for `write` command according to Vim manual, there is furthermore no mention of any "wipeout". The description of the command (`help write`) starts with "Write the whole buffer to the current file." – Armen Michaeli Oct 13 '16 at 10:44
  • This closes any splits the current buffer is on. There's a great plugin which will delete the buffer but not change your vim layout – schuess Nov 04 '16 at 14:58
  • 6
    @amn by using `:help bw` you will see `bw` is stand for wipe. – noark Jun 28 '17 at 12:59
  • @schuess Pray tell which? – Tom Hale Oct 31 '17 at 09:30
  • @TomHale plugin for killing buffers: https://github.com/qpkorr/vim-bufkill – schuess Oct 31 '17 at 13:51
  • 7
    @amn I got confused too, but he meant the `w` in the `bw` command means wipeout he is not talking about the `w` command. He put this warning there so people did not think the `bw` command meant buffer write (which it does not it means buffer wipeout.) – John Aug 24 '18 at 05:33
  • 1
    @noark @John Thanks for clarifying this, makes sense. `help bw` does take me to a completely unrelated section, but `help wipeout` works as I expected. – Armen Michaeli Aug 24 '18 at 11:55
  • If I use `:bd` or `:bw` on the last open buffer, it opens a new buffer instead. As a workaround I'm writing `function! CloseOrQuit` that uses `len(getbufinfo({'buflisted':1}))` to check the number of buffers, but this feels wrong. – John P Oct 06 '18 at 03:45
  • What I want to do (and possibly the OP) is close the buffer I am looking at, AND move to the next buffer in the list. So: `map Q :bdelete\|:bnext` – Wayne Walker Mar 04 '20 at 18:06
  • Since the bw warning can be confusing and was not moved properly during an earlier edit, I moved it at the end so that people only read it after they have read the relevant part of the answer. I hope nobody finds it more confusing somehow. – Romain Vincent Jul 16 '23 at 17:26
387

If this isn't made obvious by the the previous answers:

:bd will close the current buffer. If you don't want to grab the buffer list.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
  • 4
    Before finding :bd it never made any sense to me that people had no problems doing a :ls -> scan for doc number -> unload buffer by number. Do people actually find this effective? – Svend Aug 13 '09 at 16:08
  • 3
    @svend for a range of buffers sure, but for a lone one a :bd is probably more effective. – Pharaun Aug 12 '11 at 18:08
  • 1
    @Svend [I keep 'open' a split with a list of the buffers](https://github.com/NewAlexandria/vim.ana#sauce) – New Alexandria Jan 02 '13 at 15:30
  • 12
    `:bd` only closes the buffer whereas (whatever that means) whereas `:bw` will also clear all the variables and REALLY eliminates it from the `ls` buffer. – RedX Mar 13 '13 at 18:22
121

Check your buffer id using :buffers

you will see list of buffers there like

1  a.php
2  b.php
3  c.php

if you want to remove b.php from buffer

:2bw

if you want to remove/close all from buffers

:1,3bw
nightingale2k1
  • 10,095
  • 15
  • 70
  • 96
54

Rather than browse the ouput of the :ls command and delete (unload, wipe..) a buffer by specifying its number, I find that using file names is often more effective.

For instance, after I opened a couple of .txt file to refresh my memories of some fine point.. copy and paste a few lines of text to use as a template of sorts.. etc. I would type the following:

:bd txt <Tab>

Note that the matching string does not have to be at the start of the file name.

The above displays the list of file names that match 'txt' at the bottom of the screen and keeps the :bd command I initially typed untouched, ready to be completed.

Here's an example:

doc1.txt doc2.txt
:bd txt 

I could backspace over the 'txt' bit and type in the file name I wish to delete, but where this becomes really convenient is that I don't have to: if I hit the Tab key a second time, Vim automatically completes my command with the first match:

:bd doc1.txt

If I want to get rid of this particular buffer I just need to hit Enter.

And if the buffer I want to delete happens to be the second (third.. etc.) match, I only need to keep hitting the Tab key to make my :bd command cycle through the list of matches.

Naturally, this method can also be used to switch to a given buffer via such commands as :b.. :sb.. etc.

This approach is particularly useful when the 'hidden' Vim option is set, because the buffer list can quickly become quite large, covering several screens, and making it difficult to spot the particular buffer I am looking for.

To make the most of this feature, it's probably best to read the following Vim help file and tweak the behavior of Tab command-line completion accordingly so that it best suits your workflow:

:help wildmode

The behavior I described above results from the following setting, which I chose for consistency's sake in order to emulate bash completion:

:set wildmode=list:longest,full

As opposed to using buffer numbers, the merit of this approach is that I usually remember at least part of a given file name letting me target the buffer directly rather than having to first look up its number via the :ls command.

guv'
  • 1,359
  • 13
  • 8
51

Use:

  • :ls - to list buffers
  • :bd#n - to close buffer where #n is the buffer number (use ls to get it)

Examples:

  • to delete buffer 2:

    :bd2
    
kenorb
  • 155,785
  • 88
  • 678
  • 743
Shaun
  • 3,928
  • 22
  • 21
19

You can map next and previous to function keys too, making cycling through buffers a breeze

map <F2> :bprevious<CR>
map <F3> :bnext<CR>

from my vimrc

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Ravi
  • 191
  • 1
  • 2
8

Close buffer without closing the window

If you want to close a buffer without destroying your window layout (current layout based on splits), you can use a Plugin like bbye. Based on this, you can just use

:Bdelete (instead of :bdelete)
:Bwipeout (instead of :bwipeout)

Or just create a mapping in your .vimrc for easier access like

:nnoremap <Leader>q :Bdelete<CR>

Advantage over vim's :bdelete and :bwipeout

From the plugin's documentation:

  • Close and remove the buffer.
  • Show another file in that window.
  • Show an empty file if you've got no other files open.
  • Do not leave useless [no file] buffers if you decide to edit another file in that window.
  • Work even if a file's open in multiple windows.
  • Work a-okay with various buffer explorers and tabbars.

:bdelete vs :bwipeout

From the plugin's documentation:

Vim has two commands for closing a buffer: :bdelete and :bwipeout. The former removes the file from the buffer list, clears its options, variables and mappings. However, it remains in the jumplist, so Ctrl-o takes you back and reopens the file. If that's not what you want, use :bwipeout or Bbye's equivalent :Bwipeout where you would've used :bdelete.

airstrike
  • 2,270
  • 1
  • 25
  • 26
Wolfson
  • 1,187
  • 17
  • 22
4

[EDIT: this was a stupid suggestion from a time I did not know Vim well enough. Please don't use tabs instead of buffers; tabs are Vim's "window layouts"]

Maybe switch to using tabs?

vim -p a/*.php opens the same files in tabs

gt and gT switch tabs back and forth

:q closes only the current tab

:qa closes everything and exits

:tabo closes everything but the current tab

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
  • 2
    Why do you say that using tabs is a stupid suggestion? I only ask because I use tabs all the time... :^/ – Nate Apr 19 '12 at 23:59
  • 2
    @Nate: I decided to elaborate on that http://leonid.shevtsov.me/en/why-vim-tabs-are-different-from-any-other-editor – Leonid Shevtsov Apr 24 '12 at 11:50
  • 1
    That was a great post - it inspired me to dig even deeper - have you looked at [`wildmenu`](http://vimdoc.sourceforge.net/htmldoc/options.html#'wildmenu')? It's similar to lustyjuggler (though simpler) but it's built in (which was good for me, because lustyjuggler needs `+ruby`, and the default vim for ubuntu oneiric ships `-ruby`). – Nate Apr 26 '12 at 23:54
  • @Nate: yep, I'm using wildmenu to open files, but when you have many files in the project, it just doesn't scale. Also, the `vim-nox` package from Ubuntu has Ruby support. – Leonid Shevtsov Apr 27 '12 at 21:59
3

How about

vim -O a a

That way you can edit a single file on your left and navigate the whole dir on your right... Just a thought, not the solution...

Santi
  • 4,428
  • 4
  • 24
  • 28
1

Those using a buffer or tree navigation plugin, like Buffergator or NERDTree, will need to toggle these splits before destroying the current buffer - else you'll send your splits into wonkyville

I use:

"" Buffer Navigation                                                                                                                                                                                        
" Toggle left sidebar: NERDTree and BufferGator                                                                                                                                                             
fu! UiToggle()                                                                                                                                                                                              
  let b = bufnr("%")                                                                                                                                                                                        
  execute "NERDTreeToggle | BuffergatorToggle"                                                                                                                                                              
  execute ( bufwinnr(b) . "wincmd w" )                                                                                                                                                                      
  execute ":set number!"                                                                                                                                                                                    
endf                                                                                                                                                                                                        
map  <silent> <Leader>w  <esc>:call UiToggle()<cr>   

Where "NERDTreeToggle" in that list is the same as typing :NERDTreeToggle. You can modify this function to integrate with your own configuration.

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
  • for this, the [bbye](https://vimawesome.com/plugin/bbye) plugin is perfect which preserves your window layout when using `:bdelete` or `:bwipeout`. See [my answer](https://stackoverflow.com/a/61998749/2074869). – Wolfson May 25 '20 at 08:42