10

By default, VIM :e command will create a new buffer with specified file, leaving current buffer to hand around. This leads to buffers count growing very large (and that is not handy if some buffer display plugin is used, for example popular minibufexplorer). Is it possible to modify VIM so :e command will re-use current buffer? If i need two buffers, i can always create second one via :enew command, after all.

3 Answers 3

14

you cannot "re-use" a buffer, a buffer in vim is

a file loaded into memory for editing.

You could delete the current buffer then open a new, so that keep your buffers count:

:bd!|e /path/file

note that with !, changes on current buffer would be discarded.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Is it any documentation in VIM about this '|' notation? I wasn't aware that i can chain command like this.
11
:bd!|e file

is the most intuitive way, but it will kill your window if you have a split. So...

function! ReplaceBuffer(bang, newfile)
  let curbuf = bufnr('%')
  exec "e " . a:newfile
  exec "bd" . a:bang . " " . curbuf
endfunction
command! -nargs=1 -complete=file -bang -bar BDE call ReplaceBuffer('<bang>', <f-args>)

Then you can do

:BDE ~/.vimrc

or

:BDE! ~/.vimrc

to load up your .vimrc and kill off the buffer you were in, without messing with windows.

2 Comments

good mention that :bd!|e file kills splits! i was looking for something that preserves the split layout
@jazzyfresh, try this solution, which will preserve split windows: stackoverflow.com/questions/17611862/…
7

:e thefile | bd#

This solution has the advantage that it will preserve windows.

  1. Open "thefile" in a new buffer
  2. Delete the most recent buffer (# is the alternate buffer)

2 Comments

what if there is no most recent buffer, it will throw an error.
I had two windows open. :e filepath | bd# destroyed my other window and opened the file, therefore windows weren't preserved. Removing d# from the command preserved the windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.