2

Vim diff opens 2 files for diffing, but it doesn't navigate to the first diff section automatically on startup. Can I add something to startup script (like _vimrc) to do this?

2
  • What do you mean with "navigate to the first diff"? Jun 9, 2015 at 7:21
  • I mean jump to the first diff section, like command ]c does.
    – Thomson
    Jun 9, 2015 at 7:26

2 Answers 2

7

]c doesn't jump to the first difference, it jumps to the next difference. If your first difference is on the first line, ]c would jump to the the second one which is not what you want. One could work around that with ]c[c (jump to next difference then jump to previous).

This command:

$ vim -d -c 'norm ]c[c' filea fileb

seems to do what you want.

I don't think it is necessarily a good idea to put that in your startup sciript.

1
  • 1
    You could put that as an autocommand only when 'diff' is on, I suppose. That seems harmless enough... :help vimdiff gives an example of how (if &diff)
    – derobert
    Jun 9, 2015 at 17:21
1

Yes, building on @romainl's answer:

if &diff
  " Jump to next change, then prev change. This puts us on the first change
  " even if it is on line 1.
  autocmd VimEnter * normal! ]c[c
endif

This needs to be on VimEnter (I'm not sure why neither BufReadPost or BufEnter work, maybe someone else knows?) I used the script linked in this answer to determine the earliest event that will work correctly.

You can also add other diff-only mappings in the same block:

  " Jump to next change after getting or putting a chunk
  nnoremap do <Cmd>diffget<CR>]c
  nnoremap dp <Cmd>diffput<CR>]c

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.