sponsor Vim development Vim logo Vim Book Ad

basic Tip #622: Deleting a buffer without changing your window layout

 tip karma   Rating 93/36, Viewed by 3353 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   December 17, 2003 8:22      complexity:   basic
author:   Charles E. Campbell, Jr.      as of Vim:   5.7

Here's a small command for your <.vimrc>:

    com! Kwbd let kwbd_bn= bufnr("%")|enew|exe "bdel ".kwbd_bn|unlet kwbd_bn

To use it, type   :Kwbd

Kwbd stands for:  Keep window (layout) and delete the associated buffer.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Vim as a syntax highlighting engine for web publishing | External Paste Buffer >>

Additional Notes

Anonymous, December 17, 2003 9:45
At last!!
Anonymous, December 17, 2003 10:22
I had this need. Thank you for finding and sharing the solution.
hari_vim at yahoo dot com, December 17, 2003 19:07
You can simplify this as:

com! Kwbd enew|bw #

Hari
Anonymous, December 17, 2003 19:14
YAY!
xburgerhout AT freeler DOT nl, December 17, 2003 22:59
Great tip!

Additionally, I remapped bd to Kwbd:
map bd :Kwbd<CR>

Also, I added bn to Kwbd to automatically get the next buffer:
com! Kwbd enew|bw #|bn
[email protected], January 9, 2004 6:31
When a buffer is open in sevral windows, those other windows still get closed?!
I'm using the following at present - ugly (so many lines for a simple task) but works for me:

" Delete the current buffer, issuing bnext in all windows
" where displayed before that
function DeleteBuffer2()
   let bid = bufnr("%")
   let wid = winnr()
   windo if bid == bufnr("%") | bprev | endif
   exe "bdel " . bid
   exe "normal " . wid . "^W^W"
endfunction

" count the number of buffers
function BufferCount()
   " save cur buf number
   let cbuf = bufnr("%")
   let bnum = 0
   bufdo let bnum = bnum + 1
   " return to the buf
   exe "b " . cbuf
   return bnum
endfunction
"
"
function DeleteBuffer()
   if BufferCount() > 1
      call DeleteBuffer2()
   else
      exe "bdel"
   endif
endfunction
"
map <C-K> :call DeleteBuffer()<CR>
imap <C-K> <C-O><C-K>



http://www.cs.albany.edu, January 9, 2004 14:10
I am using the bdel tip given here, but it is very confusing, as my
original view is lost

This how bdel should work (gnu-emacs kill-buffer does it right):

> visit a file in window,
> go to 2nd file (vim gf)
> go to 3rd file
> kill-buffer of 3rd file, and you are back to the view of 2nd file,
  without disturbing windows splits / cursor position
> kill 2nd file, and viola, you are back to 1st file.

    :com! Kwbd let kwbd_bn= bufnr("%")|enew|exe "bdel ".kwbd_bn|unlet kwbd_bn  

A command should not interrupt your train of thought - Emperor Ashoka.
[email protected] - NOSPAM, February 10, 2004 8:28
To confused: I suggest that you look into  :he ctrl-o  and  :he ctrl-i
John Orr, December 1, 2004 17:27
Anyone who, like me, agrees with "http:/www.cs.albany.edu" might like to checkout my attempt to implement something closely matching that description, vimscript #1147
[email protected], April 15, 2005 11:11
Note that starting with Vim 6.0, ":bdel" does not really delete the buffer anymore but merely makes it unlisted. To really delete the buffer so that even with ":ls!" it doesn't appear, use ":bwipeout" instead.
[email protected] - NOSPAM, December 16, 2005 8:45
Here's a command that doesn't implement a buffer kill ring such as the script above implements; but it does preserve window layout.  Use

  :Kwbd    to clear the current window and only do a buffer delete if its the only instance of that buffer being displayed
  :Kwbd!   clears all windows currently displaying the current buffer, and do a buffer delete

Both of these forms of the command preserve window layout.

" ---------------------------------------------------------------------
" Kwbd: keep window layout and do a buffer delete {{{2
"    :Kwbd  -- clear current window, do buffer delete if its the only one
"    :Kwbd! -- clear all windows with current buffer and do buffer delete
com! -bang -nargs=? Kwbd set lz|call <SID>Kwbd(<bang>0)|set nolz
fun! s:Kwbd(really)
"  call Dfunc("Kwbd(really=".a:really.")")
  let kwbd   = winbufnr(0)
  let winnum = winnr()
  enew
  if a:really
   windo if winbufnr(0) == kwbd | enew | endif
   bd #
  else
   let s:cnt= 0
   windo if winbufnr(0) == kwbd | let s:cnt=s:cnt+1 | endif
   if s:cnt == 0
   bd #
   endif
  endif
  exe winnum."wincmd W"
"  call Dret("Kwbd")
endfun
[email protected] - NOSPAM, December 19, 2005 11:45
Looks like an adjustment is needed to the code snippet above...  Use
   exe "bd ".kwbd
instead of
   bd #
in the two places where its used.
[email protected], February 25, 2006 18:13
nm ,bc :bn<CR>:bd #<CR>
[email protected] - NOSPAM, February 27, 2006 8:00
Looks like emil  missed the point that if two or more windows are open on the same buffer, its a Good Idea (tm) not to delete the buffer.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo