Vim: delete buffer when exiting split window

I have this very useful function in my .vimrc:

function! MyGitDiff()
  !git cat-file blob HEAD:% > temp/compare.tmp
  diffthis
  belowright vertical new
  edit temp/compare.tmp
  diffthis
endfunction

What he does is basically open the file in which I am currently working from the repository in a vertically split window, and then compare it. This is very convenient, since I can easily compare the changes with the source file.

However, there is a problem. At the end of the comparison, I delete the split window by typing: q. This, however, does not remove the buffer from the buffer list, and I still see the compare.tmp file in the buffer list. This is annoying because whenever I make a new comparison, I get this message:

Warning: the file "temp / compare.tmp" has changed since the start of editing.

Is there a way to remove a file from buffers and also close the vertical split window?

+3
3

, bwipe?

:bw[ipeout][!] N1 N2 ...
            Like |:bdelete|, but really delete the buffer.  Everything
            related to the buffer is lost.  All marks in this buffer
            become invalid, option settings are lost, etc.  Don't use this
            unless you know what you are doing.

- :

function! DelBuf(filename)
     let bname = bufname(filename)
     if l:bname != ""
         let bidx = buffer_number(l:bname)
         exec = "bw " . l:bidx
     endif
endfunction

DelBuf("comapre.tmp") .

DelBuf `bufhidden :

autocmd! bufhidden "compare.tmp" call DelTmp("compare.tmp")

... - .

+1

diff-buffers:

  setlocal bt=nofile bh=wipe nobl noswf ro
  nnoremap <buffer> q :bw<cr>

- , (:h 'bh' → ), - .

BTW: r! git . , .

+1

autocmd winleave bd ( ). , , .

+1

Source: https://habr.com/ru/post/1782020/


All Articles