Skip to content

Instantly share code, notes, and snippets.

@Raekkeri
Last active April 2, 2021 21:17
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 Raekkeri/21f28712ffea1697a6c6839b4ea6e9ea to your computer and use it in GitHub Desktop.
Save Raekkeri/21f28712ffea1697a6c6839b4ea6e9ea to your computer and use it in GitHub Desktop.
Lightweight Git shortcuts for Vim
" Lightweight alternative to vim-fugitive:
" The following `:Git` command looks up the Git repository of the current file,
" and launches `git` in the root directory of that repository. This way, you can
" launch vim anywhere outside the Git repository, and, e.g. have open files from
" multiple Git repositories, and `:Git <args>` command always points to the
" right repository.
" One thing to note, however, is that "%" doesn't necessarily point to a correct
" path (so for example `:Git commit %` might not work), but in this case "%:p"
" should be one way to work around the issue.
command! -nargs=+ Git !git -C `git -C %:p:h rev-parse --show-toplevel` <args>
" Now, e.g. with a leader key, it is easy to configure some shortcuts:
" Show changes in current file
" noremap <Leader>d :Git diff %:p<CR>
" Show changes in all files
" noremap <Leader>D :Git diff<CR>
" Show changes to be committed in current file
" noremap <Leader>c :Git diff --cached %:p<CR>
" Show changes to be committed in all files
" noremap <Leader>C :Git diff --cached<CR>
" Quicky `git add` changes in current file
" noremap <Leader>add :Git add %:p<CR>
@David-Else
Copy link

To have the output displayed in full color in the terminal (at least in Neovim):

command! -nargs=+ Git :vsplit term://git -C `git -C %:p:h rev-parse --show-toplevel` <args>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment