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.
Add a comment
|
3 Answers
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.
1 Comment
grigoryvp
Thanks a lot! Is it any documentation in VIM about this '|' notation? I wasn't aware that i can chain command like this.
: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
jazzyfresh
good mention that
:bd!|e file kills splits! i was looking for something that preserves the split layoutwisbucky
@jazzyfresh, try this solution, which will preserve split windows: stackoverflow.com/questions/17611862/…
:e thefile | bd#
This solution has the advantage that it will preserve windows.
- Open "thefile" in a new buffer
- Delete the most recent buffer (
#is the alternate buffer)
2 Comments
Haris Muzaffar
what if there is no most recent buffer, it will throw an error.
Unknow0059
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.