Auto DiffUpdate ?

454 views
Skip to first unread message

Sylvain Chouleur

unread,
Jul 10, 2009, 5:16:41 AM7/10/09
to vim...@googlegroups.com
Hi List,

I'm looking for a sort of auto-diffupdate, I explain:

When editing in diff mode, if I modify a line that hasn't differences
with the other file, the highlight of the new differences doesn't
appears. I have to do a diffupdate to view the highlight.

Is there an option or something that refresh the highlight when editing ?

Thanks a lot.

Ben Fritz

unread,
Jul 10, 2009, 10:02:54 AM7/10/09
to vim_use


On Jul 10, 4:16 am, Sylvain Chouleur <sylvain.choul...@gmail.com>
wrote:
No option that I know of, but you can partially accomplish this using
autocmds.

Try putting the following in your .vimrc:

augroup AutoDiffUpdate
au!
autocmd InsertLeave * if &diff | diffupdate | let b:old_changedtick
= b:changedtick | endif
autocmd CursorHold *
\ if &diff &&
\ (!exists('b:old_changedtick') || b:old_changedtick !=
b:changedtick) |
\ let b:old_changedtick = b:changedtick | diffupdate |
\ endif
augroup END

This will automatically update the diff whenever you leave insertmode,
or when you haven't moved the cursor for a while and changes have been
made to the buffer since the last time this automatic diffupdate
fired.

I may run with this for a while, though I imagine it might get
annoying because of the auto-diffupdate firing after using commands
like dp and do...maybe I'll add mappings for those commands to update
the old_changedtick as well.

Ben Fritz

unread,
Jul 10, 2009, 10:18:41 AM7/10/09
to vim_use


On Jul 10, 9:02 am, Ben Fritz <fritzophre...@gmail.com> wrote:
> I may run with this for a while, though I imagine it might get
> annoying because of the auto-diffupdate firing after using commands
> like dp and do...maybe I'll add mappings for those commands to update
> the old_changedtick as well.

Here's the mappings I ended up with:

nnoremap <silent> do do:let b:old_changedtick = b:changedtick<CR>
nnoremap <silent> dp dp<C-W>w:if &modifiable && &diff \| let
b:old_changedtick = b:changedtick \| endif<CR><C-W>p

The dp mapping is interesting. From :help :diffput:

When [bufspec] is omitted and there is more than one other
buffer in diff mode where 'modifiable' is set this fails.

Since dp acts like :diffput with no range or bufspec, I tried to make
it somewhat smart about whether or not to do a diffupdate, but it's
still not going to work in many situations. It cycles to the next
window, checks if the buffer in that window is both modifiable and in
diff mode, and if so updates the old_changedtick variable. Then it
switches back to the original window.

So basically, the dp mapping will only work properly if you have
exactly two windows in diff mode, or if the next window in <C-W>w
order just happens to be the "one other buffer in diff mode where
'modifiable' is set"

Since I normally only ever diff 2 windows in Vim (for three-way diff I
tend to use external tools) this works fine for me, but YMMV.
Reply all
Reply to author
Forward
0 new messages