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

8.3k views Asked by At

I'm trying to use vimdiff as a diff tool for Git but, for some reason, the files are always open in read-only mode. It makes sense for the original file but not for the ones that I have modified (since I haven't committed them yet).

This is my current .gitconfig:

[diff]
        tool = vimdiff
[merge]
        tool = vimdiff
[difftool]
        prompt = false

Any idea what could be causing this?

3

There are 3 answers

10
Shunya On BEST ANSWER

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
5
bukzor On

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.

1
wisbucky On

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