1

I have a shared .vimrc that I use across platforms. It works fine in most circumstances, but looks terrible when I run git commit from the command line. I want to make my color scheme not load when run in that circumstance, but the conventional test for windows has("win32") fails in that environment! In fact, has("unix") is true in that environment!

Does anyone know what configuration of Vim is shipped with Git for Windows, and how to detect it?

2 Answers2

2

EDIT: user Christian Brabandt points out that v:progpath will still only contain /usr/bin/vim when invoked through git-for-windows, so this solution will not work.


Maybe something along these lines?

if v:progpath =~ "git"
    colorscheme default
endif

The variable v:progpath should contain a path to the running vim executable, and (assuming that the version of vim that git commit spawns is included in your git installation) =~ "git" should detect whether that executable is within git's installation directory. (This is all very hypothetical of course)

According to help v:progpath:

v:progpath    Contains the command with which Vim was invoked, in a form
      that when passed to the shell will run the same Vim executable
      as the current one (if $PATH remains unchanged).
      Useful if you want to message a Vim server using a
      |--remote-expr|.
      To get the full path use: 
          echo exepath(v:progpath)
      If the command has a relative path it will be expanded to the
      full path, so that it still works after `:cd`. Thus starting
      "./vim" results in "/home/user/path/to/vim/src/vim".
      On Linux and other systems it will always be the full path.
      On Mac it may just be "vim" and using exepath() as mentioned
      above should be used to get the full path.
      On MS-Windows the executable may be called "vim.exe", but the
      ".exe" is not added to v:progpath.
      Read-only.
tboz203
  • 131
  • 4
  • 1
    Welcome to [vi.se]! This could benefit from some explanation, esp. the v:progpath variable (citing the help is fine!). For example, does v:progpath usually contain git when vim is started by git-commit on Windows? Is that reliable? – D. Ben Knoble Jun 10 '21 at 18:08
  • v:progpath is the path to the actual running vim instance. In Git-For-Windows, this is something like /usr/bin/vim. It has nothing to do with git itself, just that git-for-windows packages vim along with many other POSIX like utilities. – Christian Brabandt Jun 11 '21 at 08:41
1

A new angle of attack:

if has_key(environ(), "MINGW_PREFIX") && has_key(environ(), "GIT_INDEX_FILE")
    colorscheme default
endif

Poking around in help environ, it looks like has_key(environ(), <name>) is the recommended way of determining whether a system environment variable is defined. I'm not an expert on what variables will or won't exist during git commit, but MINGW_PREFIX looks like its an artifact of the unix compatibility layer git-for-windows uses (worth a google), and GIT_INDEX_FILE looks like its provided by git itself (per the docs). Again, highly theoretical, YMMV!

tboz203
  • 131
  • 4