10

I am unable to configure git to follow my requests:

  1. use vim as a diff pager
  2. keep colours for diff when adding files in interactive mode

My ~/.gitconfig setup:

[color]
    ui = auto
    # diff = false

[pager]
    diff = vim -

With this configuration the interactive mode for git add --interactive produces coloured output as expected:

interactive adding

The downside of this is that diff in vim is corrupted. See the output of git diff:

diff using vim

When using git diff | vim - the colours are OK but I'm too lazy to type the full command. Is there any known method that preserves colours in both cases?

Ikar Pohorský
  • 4,617
  • 6
  • 39
  • 56
  • See http://stackoverflow.com/questions/39470081/vim-check-if-running-as-a-pager on how to automatically execute :AnsiEsc upon load. – 0fnt Sep 14 '16 at 03:01

3 Answers3

14

You need to install AnsiEsc plugin and run :AnsiEsc just after loading file. You can set pager to vim -c AnsiEsc - to do this.

Alternatively, use vim own highlighting: using the same method (-c command) run

%sm/\e.\{-}m//g
set ft=diff

:

    diff = "vim -c '%sm/\\e.\\{-}m//g' -c 'set ft=diff' -"
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • 3
    perfect, thanks! I used a slightly improved version that sets the cursor at the top of the buffer after substitution. It also ignores empty diffs: _diff = "vim -c '%sm/\\e.\\{-}m//ge' -c 'set ft=diff' -c 'normal gg' -"_ – Ikar Pohorský Jun 11 '13 at 16:26
  • 1
    I added -c 'setlocal buftype=nofile' to prevent vim from complaining about unsaved changes when trying to :q – notlesh Jul 21 '16 at 19:39
4

You have two alternatives here.

Use Vimdiff as a difftool

$ git config --global diff.tool vimdiff
$ git config --global difftool.prompt false
$ git config --global alias.d difftool

[diff]
    tool = vimdiff
[difftool]
    prompt = false
[alias]
    d = difftool # Not needed, just a convenience. $ git difftool, still works.

Regular Git diff, but with color highlithing.

$ git config --global color.ui true

[color]
    ui = true

Right now, you have a weird mix. You can of course use both, but the pager is messing up things.
The reason you get ^[32m etc is because you're trying to read shell colors in Vim. This doesn't work.

Community
  • 1
  • 1
timss
  • 9,982
  • 4
  • 34
  • 56
0

Try these settings:

git config --global diff.tool vimdiff
git config --global merge.tool vimdiff

git config --global difftool.prompt false
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130