Skip to content

Instantly share code, notes, and snippets.

@maxim
Created September 18, 2011 19:34
Show Gist options
  • Select an option

  • Save maxim/1225452 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/1225452 to your computer and use it in GitHub Desktop.
Delete buffer if last window, otherwise close window
" Closes window if there are more windows opened to the same buffer.
" Otherwise deletes buffer.
fun! WinQuitOrBufDelete ()
let win_cnt = 0
for wnr in range(1, winnr("$"))
if winbufnr(wnr) == bufnr("%")
let win_cnt += 1
endif
if win_cnt == 2
close
return
endif
endfor
bd
endf
@SethMilliken

Copy link
Copy Markdown
function! CloseBuffer()
  let references = 0
  for window in range(1, winnr("$"))
    let references += (winbufnr(window) == bufnr("%")) ? 1 : 0

    if references == 2
        echo 'hiding the window'
        return
    endif
  endfor
  echo 'deleting this buffer'
endfunction

@Raimondi

Copy link
Copy Markdown
function! CloseBuffer()
  if len(filter(range(1,winnr('$')), 'v:val != bufnr("%")')) > 1
    echom 'hiding the window'
  else
    echom 'deleting this buffer'
  endif
endfunction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment