28

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 Answers 3

24

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.

Sign up to request clarification or add additional context in comments.

5 Comments

this maybe some error, directly add "set noreadonly" in your vimrc file.
@Procras: I think you accidentally a word.
@alexis you still missed starting double quote for last word. please add it to make copy/paste safe for further users
@ZuBB yes!, thanks. I can't edit the old comment so I'll write it here again: I just put [difftool "vimdiff"] cmd = vim -f -d -c 'wincmd l' -c 'cd "$GIT_PREFIX"' "$LOCAL" "$REMOTE" in ~/.gitconfig
Instead of removing it, I'd consider replacing the -R flag with the -n flag to allow writing, but disable swap files. This makes recovery after crash impossible, but avoids weird errors & behavior when diffing files opened in other vim sessions.
18

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

10 Comments

Do you know if there's any way to set up vimdiff so that this option is always on?
In your vimrc file, construct the following line: set noro
Would you consider expanding this answer to explain why it's the default desired behavior?
Read-only is not a default behavior of vimdiff. Try it: vimdiff thisfile thatfile. Git passes -R (Read-only mode) when invoking vimdiff. Is there a way to tell git to not do that?
@BAdhi "What happens if you remove the read only option and accidentally edit a file in a previous commit?" It doesn't matter, because it's just a copy of the file in a tmp dir. It is impossible to change a previous git commit.
|
10

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
    

1 Comment

The first option is safe and simple.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.