1

I use the solution provided here, reproduced below:

function! s:DiffWithSaved()
  let filetype=&ft
  diffthis
  vnew | r # | normal! 1Gdd
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffSaved call s:DiffWithSaved()

to visually diff a buffer I am currently working on with the saved version on disk. Suppose my buffer list contains one file, file1.cpp (this is the only file that appears on :ls). Suppose I modify this buffer and obtain file1.cpp+. + here indicates that the buffer contents have diverged from the version saved on disk. On calling :DiffSaved, the output of :DiffSaved appears on the full screen where the screen is split so (x is the cursor position):

________________
 .cpp  | .cpp+  |
       |        |
 x     |        |
_______|________|

That is, the file on disk appears on left where the cursor is.

On this new tab(?), if I run :ls, I still see only file.cpp+. Aren't the two windows of the output of DiffSaved also part of the buffer list? Is the output of DiffSaved on some temporary space elsewhere? Is it possible to have the output so where the current file's buffer appears on the left so that a :w saves the file if I am satisfied with the diff? As of now, to save the buffer, either I have to close this diff tab via :q and issue a :w on the underlying file buffer. Or else, one has to navigate right to the modified buffer in the diff output tab and write buffer to file.

Preferred diff output layout
________________
 .cpp+ | .cpp   |
       |        |
 x     |        |
_______|________|

1 Answer 1

2
  1. You've asked for it (tm). set nobl implies that buffer will NOT be shown in :ls output. See :h 'buflisted', :h :ls. Do not copy/paste so blindly.

  2. By default vertical splits are opened with respect to :h 'splitright' option (defaults to unset). Either set splitright globally or overwrite it for a particular command with :h :rightbelow modifier.

  3. vnew et al. always obtain focus. If you don't do anything to go back (e.g. wincmd p) you'll stuck in the newly created window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.