Skip to content

Instantly share code, notes, and snippets.

@shmup
Last active August 25, 2022 21:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shmup/d5bc215d4647dde36d52d9571709e890 to your computer and use it in GitHub Desktop.
Save shmup/d5bc215d4647dde36d52d9571709e890 to your computer and use it in GitHub Desktop.
Quick Git & Fugitive.vim for WSL2

Quick Git & Fugitive.vim for WSL2

Some things that may help you.

Keep in mind that my solution caters to both being in a /mnt/* path, and also being in my symlink path.

When you make a symbolic link, as in: ln -s /mnt/c/Users/bar $HOME/bar and cd into that, the $PWD will reflect that.

.bashrc improvement

This simply uses git.exe or /usr/bin/git depending on your $PWD

# This is for your .bashrc

## checks to see if we are in a windows or linux dir
function isWinDir {
  case $PWD/ in
    /mnt/*) return $(true);;
    ## if you like to use a symlink to /mnt/c/Users/bar you gotta do this..
    /home/foo/bar/*) return $(true);;
    *) return $(false);;
  esac
}
## wrap the git command to either run windows git or linux
function git {
  if isWinDir
  then
    git.exe "$@"
  else
    /usr/bin/git "$@"
  fi
}

.vim/after/plugin/fugitive.vim improvement

This is a technique to clean up the path slashes and use git.exe if in /mnt (or symlink)

" Put this in .vim/after/plugin/fugitive.vim

function! StartsWith(longer, shorter) abort
  return a:longer[0:len(a:shorter)-1] ==# a:shorter
endfunction

function! InWindows() abort
  " /home/jtm/jtm is a symlink to /mnt/c/Users/jtm
  return StartsWith($PWD, '/mnt') || StartsWith($PWD, '/home/jtm/jtm')
endfunction

function! FugitiveGitPath(path) abort
  if InWindows()
    let g:fugitive_git_executable = '/mnt/c/PROGRA~1/Git/cmd/git.exe'

    return substitute(a:path, '^/mnt/\(\a\)/', '\1:/', '')
  endif

  return a:path
endfunction

function! FugitiveVimPath(path) abort
  if InWindows()
    return substitute(a:path, '^\(\a\):/', '/mnt/\1/', '')
  endif

  return a:path
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment