0

I have a few options I like to apply both when Vim is opened in diff mode and when it is switched to diff mode, e.g., when calling diffsplit.

I have the following lines in my ~/.vimrc:

command! -nargs=* Wrap set wrap linebreak nolist

function! Set_diff_mode_options()
    syntax off
    set diffopt=filler,internal,algorithm:histogram,indent-heuristic
    autocmd VimEnter * windo Wrap .
    autocmd VimEnter * windo set nofoldenable
    autocmd VimEnter * wincmd p
endfunction

" When loading vim in diff mode, e.g. vimdiff
if &diff
    call Set_diff_mode_options()
endif

" When changing to diff mode while vim is open
autocmd OptionSet diff call Set_diff_mode_options()

When Vim starts in diff mode, all lines of the function are applied correctly. However, when OptionSet is activated in an existing Vim windows, the autocmds defined in the function seems to be ignored.

How can I ensure the function works properly in both cases?

UPDATE 1: It looks like replacing VimEnter with CursorMoved resolves the issue for diffsplit, but I suspect it will be inefficient because it performs the same operations many times as I am exploring the diff.

1 Answer 1

1

In the solution you have tried the OptionSet triggers the autocmd that are linked to VimEnter

But it is too late. The VimEnter event has already been triggered and it doesn't work.

But for OptionSet there are no reason to delay the changes.

I would propose to have two functions:

  1. Set_diff_mode_options_post
  2. Set_diff_mode_options

The first delays the application of the option after the .vimrc is read and the Windows created for the diff mode.

The second applies directly the options.

command! -nargs=* Wrap set wrap linebreak nolist


function! Set_diff_mode_options_post()
    syntax off
    set diffopt=filler,internal,algorithm:histogram,indent-heuristic
    autocmd VimEnter * windo Wrap .
    autocmd VimEnter * windo set nofoldenable
    autocmd VimEnter * wincmd p
endfunction

" When loading vim in diff mode, e.g. vimdiff
if &diff
    call Set_diff_mode_options_post()
endif

function! Set_diff_mode_options()
    if &diff
      syntax off
      set diffopt=filler,internal,algorithm:histogram,indent-heuristic
      Wrap .
      set nofoldenable
    endif
endfunction

" When changing to diff mode while vim is open
autocmd OptionSet diff call Set_diff_mode_options()
3
  • 1
    Explaining why the VimEnter didn't work in the OP would improve this answer
    – D. Ben Knoble
    Commented Aug 17, 2022 at 13:47
  • Is it possible to return the focus to the first window, similar to the wincmd p line in the question?
    – Matt
    Commented Aug 17, 2022 at 19:15
  • The OptionSet is triggered for both windows, we would need a way to identify the "first" one. I don't know how to do that in a reliable way. Commented Aug 17, 2022 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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