I want a live view of diffs while I am editing a file. vimdiff is able to do something I want, like highlighting the differences between two files.

However there are two drawbacks of vimdiff that holds me back.

First, it cannot buffer the same file independently to allow a diff comparison. This problem can be easily worked around. I have the following shell script (call it diffvim) to make vimdiff buffer the same file independently (by making a temporary copy of the file under editing).


# Differentially Viming

FILEBN=$(basename $FILENM)

if [ ! -d "$TMPDIR" ]; then
  mkdir $TMPDIR


The second drawback of vimdiff is that editing the non-diff parts of one of the two copies of the same file does not trigger diff to recalculate the differences. One has to do it manually by invoking :diffupdate. I want this recalculation to be automated. The relatively convenient way I feel is to trigger it when I exit the INSERT mode and enter the NORMAL mode. This again can be easily done by adding the following mapping to my .vimrc file:

inoremap <Esc> <Esc>:diffu<CR>

However, it is not the perfect solution yet. Because if I do some editing in the NORMAL mode, that is without entering and then exiting the INSERT mode, recalculation will still not be triggered automatically after the editing is done. So I add another mapping to my .vimrc file:

nnoremap <Esc> :diffu<CR>

But this time, something strange happens. When I open a file for editing, some of the standard key bindings malfunction. For example, if I press key h or l (that is, any of them being the first command I issue after opening the file), all get me into INSERT mode, while key j or G delete a line or the whole content, etc. But if after opening the file, I press key Esc first, then those commands work fine.

Any idea what goes wrong? Do you have a work-around?


After looking into autocmd in vimdoc, I think I have found the solution that does exactly what I want:

autocmd CursorMoved,CursorMovedI * diffupdate

Many thanks to Ingo for his suggestion!


<Esc> is also used in several keycodes of special keys, and it plays a role in printing to the terminal.

It's a bad idea to overload it, especially with something so invasive like you did. Use a different key, e.g. <Leader><Leader>, or define a trigger on events:

:autocmd InsertEnter,InsertLeave * diffupdate
  • Thanks for the answer. This kind of event trigger is cleaner solution. Can I do it also in the NORMAL mode. BTW, I have detailed my question.
    – day
    Apr 12 '13 at 21:06
  • Ah, I think I have found the solution. Add also the event CursorMoved.
    – day
    Apr 12 '13 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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