3

99% of the time when I am opening a diff (using the :diffsplit command), it's because I want to reference some code, but still edit the file I started in. However, whenever you open a diff with diffsplit, the new file gets focus. I'd like to make vim automatically switch back (Or not switch in the first place if that's an option). I didn't see anything about it in diffopt so I tried an autocmd to accomplish this but to no avail:

autocmd FilterWritePost * if(&diff) | wincmd w

Any ideas on how to accomplish this?

  • 3
    You want to use wincmd p to go to the previous window. – Tommy A Jul 6 '16 at 12:55
  • No dice. It still has no effect. – Tumbler41 Jul 6 '16 at 13:00
  • how do you "open a diff"? – Christian Brabandt Jul 22 '16 at 18:06
  • Using the :diffsplit command. – Tumbler41 Jul 22 '16 at 18:07
7

You could add this to your .vimrc:

command! -nargs=1 -complete=file Diffsplit diffsplit <args> | wincmd p

This create a Diffsplit command (note the capitalized D) which takes one argument and accept file names as completion.

The command will create the diffsplit with the argument provided and use the command windcm p to go back to the initial file.

See :h user-commands and :h wincmd.

  • This is a good workaround, but I'd like to see if anyone can come up with an "official" solution. – Tumbler41 Jul 22 '16 at 17:57
  • 1
    I don't really understand what you mean by an "official" solution? – statox Jul 25 '16 at 8:24
  • Sorry, what I should have said was, "able to use the vim command :diffsplit." So not using an alias command. But it looks like this isn't possible. So I'll probably wait a few more days then give it to you if nothing else shows up. Thanks. :) – Tumbler41 Jul 25 '16 at 14:09
  • @Tumbler41 Oh ok I get it now. Well maybe someone will come up with a clever solution which will not require a custom command but I'm really not sure that is possible :-) – statox Jul 26 '16 at 7:11
4
+50

Here you can find the source code defining the diffsplit command: vim/vim, let see what it does:

  1. It set some reference to the old window, as we will create a new one.
  2. It disable the tab opening
  3. Split the window (create a new window, that where the old_curwin is useful)
  4. Edit the desired file
  5. Check that we are on a new window
  6. Set the diffs options for the current window
  7. Check the old window is still a valid window
  8. Set the diffs options for the old window
  9. Check the old buffer reference is still valid
  10. Change the line number to match the one of the old window.

As you can see, there is nothing in this function that change the cursor from the new window to the original one. I think the official* answer is that "it's by design that you stay on the new window".

Now, you can, as other answer suggested, use an autocommand to add the switch to original step, I think it's the best you can do.

* From what I understand, I wont talk for the official Vim maintainers.


Why your autocomand does not work:

When you do

autocmd FilterWritePost * ...

And it is called from the diffsplit command, both files will trigger this event (and both files will have the &diff variable set), you can check that with the following autocommand:

:autocmd FilterWritePre * if &diff | echomsg expand('%')

I think your best solution is still to use a custom command.


I have tried to work with the following autocommand events: BufEnter, BufWinEnter, OptionSet, WinEnter.

But none of them gave results, because it gets triggered two times, or because it gets triggered before the buffer has the diff option set.

Once more, I think your best solution is to use a custom command.

  • An autocmd would be great. If you look at the question you'll see that I tried to create one, but couldn't get it to work. Could you give an example of an autocmd that would do this? – Tumbler41 Jul 23 '16 at 15:30
  • Updated, I don't think an autocommand is suited for this :/ – nobe4 Jul 23 '16 at 16:30

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.