I'm looking for a command to delete a range of buffers.

I launch vim which opens a new unnamed buffer.
I then switch to a file on which I previously put the mark N (~/Dropbox/notes).

In this file I've written:

:e /tmp/foo
:e /tmp/bar
:e /tmp/baz
:e /tmp/qux
:e /tmp/norf

I visually select those five lines, and source them (:<c-u>@*).

Now I've got 5 new buffers.

I switch back to the N file and do a listing (ls):

  1  h   "[No Name]"                    line 1
  2 %a   "Dropbox/notes"                line 139
  3  h   "/tmp/foo"                     line 1
  4  h   "/tmp/bar"                     line 1
  5  h   "/tmp/baz"                     line 1
  6  h   "/tmp/qux"                     line 1
  7 #h   "/tmp/norf"                    line 1

I want to delete the 5 temporary buffers from 3 to 7.
Most Ex commands accept a range, and I suppose :bd is probably one of them. I may have misunderstood the help but it seems to confirm you can prefix :bd with a range :

:N,Mbdelete[!] Do ":bdelete[!]" for all buffers in the range N to M inclusive.

So, I type :3,7bd but the result is not what I expected.

All the buffers from 2 to 7 were deleted, including my N file and I'm automatically sent back to the unnamed buffer n° 1. Here's the final output of :ls:

  1 %a   "[No Name]"                    line 1

I tried other ranges like 5,6 but the result is always the same, everything is unloaded except the first buffer.

Is it possible to pass a range to the :bd command, and if so how ?


I think I've found the cause of the problem, but I don't understand it. It seems to happen only inside a buffer in which a fold has been created.

Here's how to reproduce it:

  • create a file /tmp/test in which you put : :e /tmp/foo :e /tmp/bar :e /tmp/baz :e /tmp/qux :e /tmp/norf
  • launch vim with an empty vimrc and edit the file /tmp/test:

    vim -u NONE /tmp/test

  • source it to open 5 new buffers: :so%
  • go back to first buffer (/tmp/test): :b1
  • fold the whole buffer: ggzfG
  • type :ls: 1 %a "/tmp/test" line 1 2 "/tmp/foo" line 1 3 "/tmp/bar" line 1 4 "/tmp/baz" line 1 5 "/tmp/qux" line 1 6 # "/tmp/norf" line 1

  • type :3,4bd to delete buffers from 3 to 4 (bar and baz).

On my system, vim deletes 5 buffers (1 to 5) instead of 2.

After that the output of :ls is: 6 %a "/tmp/norf" line 1

If I don't fold the buffer, the :bd command interprets the range as expected.

I'm using vim version 7.4.942 installed from this ppa.

Why is the behavior of the :bd command modified when executed inside a buffer in which a fold has been created ?

  • what vim version is this? – Christian Brabandt Dec 13 '15 at 14:07
  • 1
    @ChristianBrabandt I'm using vim version 7.4.942. I've edited my question because I think the problem comes from creating a fold inside a buffer. – saginaw Dec 13 '15 at 16:20
  • The question (and its editing) was quite insightful, hope more people will upvote it :) – VanLaser Dec 14 '15 at 0:27

This is mentioned briefly at :h fold-behaviour

For Ex commands the range is adjusted to always start at the first line of a
closed fold and end at the last line of a closed fold.  Thus this command:
when used with the cursor on a closed fold, will replace "foo" with "bar" in
all lines of the fold.

I can see, how this is confusing, but I am not sure, if this qualifies as a bug.

| improve this answer | |
  • 1
    To see if I got that right, if I have a fold from lines 2 to 10, and I run :3,4bd, this actually gets replaced to something equivalent to :2,10bd ? – VanLaser Dec 13 '15 at 18:13
  • 2
    yes, i believe this is what happens. – Christian Brabandt Dec 13 '15 at 18:35
  • 3
    In any case, I just made a patch. Let's see what Bram thinks. – Christian Brabandt Dec 13 '15 at 19:27
  • @ChristianBrabandt Thank you very much for the patch ! I really hope it will be accepted, because it's really annoying for someone who uses folds. Even outside of the fold, the range is still modified. Inside the test buffer, when I add " hello world ! on a new line, under the fold, and put the cursor under it to be clearly outside of the fold, :3,4bd is still modified to :1,5bd. If you've got favorite files on which you put some marks, and you replace :bd with :bw you can lose all your marks. – saginaw Dec 13 '15 at 20:00
  • 1
    @saginaw It does not really matter, where the cursor is, just if the range given falls into a folded region. – Christian Brabandt Dec 14 '15 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.