grigoryvp
grigoryvp

Reputation: 42613

VIM: How to open new file in current buffer?

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.

Upvotes: 10

Views: 17031

Answers (3)

wisbucky
wisbucky

Reputation: 38033

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

Upvotes: 7

Amadan
Amadan

Reputation: 198526

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

Upvotes: 11

Kent
Kent

Reputation: 195269

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.

Upvotes: 14

Related Questions