I have this issue where I need to both compare files, which I do using vimdiff, but also work on individual files with vertically split screens (vs). However, after I call vimdiff on a file, opening the file again later I see that scrollbind and cursorbind are set, which gets annoying to deactivate manually all the time. Is there a way to turn off scrollbind and cursorbind off automatically on exit form vimdiff?

As a clarification, here's an example of what I experience:

$ vim foo.txt             # works fine, noscrollbind and nocursorbind are set
$ vimdiff foo.txt bar.txt # works as expected, scrollbind and cursorbind are set during diff
$ vim foo.txt             # now scrollbind and cursorbind remain set, which I don't want
  • 1
    See if :diffoff solves the problem. Will post an answer if it works for you. Add ! if you want it to effect all windows (in current tabpage). – B Layer Jun 16 at 18:06
  • It doesn't. Also I know how to set noscrollbind and set nocursorbind, but I don't want to do it every time manually, as the steps I outlined above are part of a workflow that I use very often. – mivkov Jun 16 at 18:08
  • Yes, I just tried it. – mivkov Jun 16 at 18:10
  • That didn't change the outcome either. – mivkov Jun 16 at 18:12
  • Let us continue this discussion in chat. – B Layer Jun 16 at 18:12

There's a command that is made to deal with exactly that: :diffoff

It's effect on the current window...

    'diff'      off
    'scrollbind'    off
    'cursorbind'    off
    'scrollopt' without "hor"
    'wrap'      on
    'foldmethod'    "manual"
    'foldcolumn'    0

In other words it reverses all the settings put in place by :diff.

Now in your case we've encountered a bit trickiness. The command isn't working for you, you've reported, and that's apparently due to the fact that you're running the diff from the command line with vimdiff.

Fortunately, there's a variation of the command that will extend its effects to all windows (in the current tabpage): :diffoff! (the difference is the bang ! appended to the command). You've reported success with this.

Update: Ah, thanks to DBK's insightfulness we've solved the mystery of why 'scrollbind' and 'cursorbind' are getting preserved when a previously closed diff'ed file is reopened. (Which don't sound right.)

@mivkov is actually doing mkview in an autocommand and thus those two settings are persisted. The key line is basically this:

autocmd BufWinLeave * mkview 

And, fortuitously, this actually lends itself to a bonus for the OP...an easy way to automate the running of :diffoff which they weren't too keen on doing manually. Problem solved with a bit of a modification to that auto command:

autocmd BufWinLeave * if &diff | diffoff | endif | mkview

And everyone goes home happy!

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.