Skip to content

Instantly share code, notes, and snippets.

@doitian
Last active August 10, 2023 12:57
Show Gist options
  • Save doitian/4c849956f5c97bd1115351142d446853 to your computer and use it in GitHub Desktop.
Save doitian/4c849956f5c97bd1115351142d446853 to your computer and use it in GitHub Desktop.
Open history version files from Git in Vim via LazyGit
# yaml-language-server: $schema=https://json.schemastore.org/lazygit.json
promptToReturnFromSubprocess: false
os:
editPreset: "nvim"
customCommands:
- key: E
context: commitFiles
subprocess: true
# - When a file is selected, open the file before the commit
# - When a directory is selected, open the commit changes limited to that directory
command: |
{{if .SelectedCommitFile}}
git show '{{.SelectedLocalCommit.Sha}}^:{{.SelectedCommitFile.Name}}' |\
vim -Rm - +"let b:fname = 'file git://{{.SelectedLocalCommit.Sha}}^/{{.SelectedCommitFile.Name}}'" \
{{else}}
git show '{{.SelectedLocalCommit.Sha}}' -- git show {{.SelectedCommitFilePath}} |\
vim -Rm - +"let b:fname = 'file git://{{.SelectedLocalCommit.Sha}}/{{.SelectedCommitFilePath}}.patch'" \
{{end}}--cmd 'set mls=0' \
+'nnoremap <buffer> q <Cmd>quit<CR>' \
+"exec 'file '.fnameescape(b:fname) | unlet b:fname | set mls=5 | silent! filetype detect"
@doitian
Copy link
Author

doitian commented Aug 10, 2023

  • When choosing a file in the commit pannel: open the file before the changes in that commit in Vim.
  • When choosing a directory in the commit pannel: open the patch of that directory in Vim.

The gotchas to open stdin in vim/nvim

  • Ignore the modeline errors by ignoring first and running filetype detect with silent!.
  • Use fnameescape to escape the file name since Vim tries to expand %, # and other symbols in cmdline.
  • -M does not work well with plugin like Telescope.

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