How to prevent git vimdiff from opening files as read-only?

The deafult command that git uses for vimdiff is: (found by inspecting process list)

vim -R -f -d -c "wincmd l" -c 'cd "$GIT_PREFIX"' "$LOCAL" "$REMOTE"

You can override this (to not use -R, readonly mode) by setting the difftool.vimdiff.cmd variable.

$ git config --global difftool.vimdiff.cmd 'vim -f -d -c "wincmd l" -c '\''cd "$GIT_PREFIX"'\'' "$LOCAL" "$REMOTE"'

The quoting is tricky. I would copy-paste it.


The reason this happens is because git invokes vimdiff with the -R (readonly) option. There are several solutions to be able to write to the file:

  1. Use :w! in vim. That will force write even though it was opened as readonly.

  2. You can edit ~/.gitconfig to override the vimdiff command without -R

    [difftool "vimdiff"]
    cmd = vimdiff "$LOCAL" "$REMOTE"
    
  3. You can edit ~/.vimrc to always make vimdiff writeable. (This will affect all vimdiff, not just git.)

    if &diff
        set noreadonly
    endif
    

That's the default desired behaviour for vimdiff. You can unset using :set noro.

Or in your .vimrc config, add this:

" Default to not read-only in vimdiff
set noro