[vim/vim] In .vimrc, how to tell if gvim is under git 3way merge or not? (Discussion #15785)

18 views
Skip to first unread message

flyingosprey

unread,
Oct 3, 2024, 1:04:32 AM10/3/24
to vim/vim, Subscribed

On Windows, I ran

git config --global --replace-all merge.tool gvimdiff
git config --global --replace-all mergetool.gvimdiff.path "C:/Program Files/Vim/vim90/gvim.exe"
git config --global --replace-all mergetool.gvimdiff.trustExitCode false
git config --global --replace-all merge.conflictstyle diff3

to set gvim as git mergetool.

I want to set a different color when git merge. I found

if &diff
colorscheme codedark
endif

does not work because when git merge starts, &diff is 0.

Questions: In .vimrc, how to tell if gvim is under git 3way merge or not?

Thanks for your suggestion.


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/15785@github.com>

Gary Johnson

unread,
Oct 3, 2024, 4:56:00 PM10/3/24
to reply+ACY5DGD5DJJMWJBM34...@reply.github.com, vim...@googlegroups.com
On 2024-10-02, flyingosprey wrote:
> On Windows, I ran
>
> git config --global --replace-all merge.tool gvimdiff
> git config --global --replace-all mergetool.gvimdiff.path "C:/Program Files/Vim
> /vim90/gvim.exe"
> git config --global --replace-all mergetool.gvimdiff.trustExitCode false
> git config --global --replace-all merge.conflictstyle diff3
>
> to set gvim as git mergetool.
>
> I want to set a different color when git merge. I found
>
> if &diff
> colorscheme codedark
> endif
>
> does not work because when git merge starts, &diff is 0.
>
> Questions: In .vimrc, how to tell if gvim is under git 3way merge or not?
>
> Thanks for your suggestion.

The only way I use git on Windows is from Cygwin or from Git for
Windows, so these comments apply to those environments.

I'm surprised that "if &diff" doesn't work. In my ~/.gitconfig, the
vimdiff mergetool is this:

cmd = "vim -d -c '4wincmd w | wincmd J | wincmd =' $LOCAL $BASE $REMOTE $MERGED"

The -d option sets &diff before .vimrc is sourced.

A few ideas come to mind:

1. After git opens gvim, execute

:echo $GIT<tab>

to complete all the environment variables in gvim's environment
that begin with "GIT".

Alternatively, shell out, then look for those variable from the
shell, e.g.,

:sh
$ env | grep GIT

See if any of those variable names or values are unique to the
merge command. If some are, then test their existence and/or
values in your .vimrc.

2. Wait to test the value of &diff until gvim is finished opening
files, e.g.,

autocmd VimEnter if &diff | colorscheme codedark | endif

That has the problem that it will set your colorscheme for any
vimdiff command.

3. Expand the VimEnter autocommand above to either count the number
of windows (winnr("$")) and check that the number is 4, or
traverse the open windows and check that &diff is set in all of
them.

4. Examine the v:argv variable and look for a pattern in the
arguments that might be unique to the git merge command.

HTH,
Gary

vim-dev ML

unread,
Oct 3, 2024, 4:56:28 PM10/3/24
to vim/vim, vim-dev ML, Your activity

On 2024-10-02, flyingosprey wrote:
> On Windows, I ran
>
> git config --global --replace-all merge.tool gvimdiff
> git config --global --replace-all mergetool.gvimdiff.path "C:/Program Files/Vim
> /vim90/gvim.exe"
> git config --global --replace-all mergetool.gvimdiff.trustExitCode false
> git config --global --replace-all merge.conflictstyle diff3
>
> to set gvim as git mergetool.
>
> I want to set a different color when git merge. I found
>
> if &diff
> colorscheme codedark
> endif
>
> does not work because when git merge starts, &diff is 0.
>
> Questions: In .vimrc, how to tell if gvim is under git 3way merge or not?
>
> Thanks for your suggestion.

The only way I use git on Windows is from Cygwin or from Git for
Windows, so these comments apply to those environments.

I'm surprised that "if &diff" doesn't work. In my ~/.gitconfig, the
vimdiff mergetool is this:

cmd = "vim -d -c '4wincmd w | wincmd J | wincmd =' $LOCAL $BASE $REMOTE $MERGED"

The -d option sets &diff before .vimrc is sourced.

A few ideas come to mind:

1. After git opens gvim, execute

:echo $GIT<tab>

to complete all the environment variables in gvim's environment
that begin with "GIT".

Alternatively, shell out, then look for those variable from the
shell, e.g.,

:sh
$ env | grep GIT

See if any of those variable names or values are unique to the
merge command. If some are, then test their existence and/or
values in your .vimrc.

2. Wait to test the value of &diff until gvim is finished opening
files, e.g.,

autocmd VimEnter if &diff | colorscheme codedark | endif


That has the problem that it will set your colorscheme for any
vimdiff command.

3. Expand the VimEnter autocommand above to either count the number
of windows (winnr("$")) and check that the number is 4, or
traverse the open windows and check that &diff is set in all of
them.

4. Examine the v:argv variable and look for a pattern in the
arguments that might be unique to the git merge command.

HTH,
Gary


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/15785/comments/10837458@github.com>

flyingosprey

unread,
Oct 3, 2024, 11:50:34 PM10/3/24
to vim/vim, vim-dev ML, Comment

Thanks.

    autocmd VimEnter if &diff | colorscheme codedark | endif

does not work.

I try "echo v:argv", which shows that by default gvim does not start with "-d". And the diff happen after load. I think that's why &diff does not work

"echo $GIT" have some info. But it's not cross platform for Windows/Linux/Mac

Thanks, anyway.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/repo-discussions/15785/comments/10839562@github.com>

flyingosprey

unread,
Oct 4, 2024, 1:14:25 AM10/4/24
to vim/vim, vim-dev ML, Mention

Hi @vim-ml ,

For cmd = "vim -d -c '4wincmd w | wincmd J | wincmd =' $LOCAL $BASE $REMOTE $MERGED"

Do you know how to use " git config --global --replace-all XXX" to make a global setting?

Thanks.


Reply to this email directly, view it on GitHub.

You are receiving this because you were mentioned.Message ID: <vim/vim/repo-discussions/15785/comments/10839968@github.com>

D. Ben Knoble

unread,
Oct 6, 2024, 10:26:20 PM10/6/24
to vim/vim, vim-dev ML, Mention

Just a note that using (g)vimdiff as a mergetool has a rather complicated (though useful) setup sequence that involves parsing a custom layout string. So you'd probably need a more specific check of v:argv to match with the invocation Git actually uses when running git mergetool -t gvimdiff.


Reply to this email directly, view it on GitHub.

You are receiving this because you were mentioned.Message ID: <vim/vim/repo-discussions/15785/comments/10862211@github.com>

Reply all
Reply to author
Forward
0 new messages