Skip to content

Instantly share code, notes, and snippets.

@treatmesubj
Last active September 9, 2024 17:06
Show Gist options
  • Save treatmesubj/efc377f8538aa25e91309854411cc95b to your computer and use it in GitHub Desktop.
Save treatmesubj/efc377f8538aa25e91309854411cc95b to your computer and use it in GitHub Desktop.
vimdiff a file against a main branch or a prior commit, while actively editing the file
#!/usr/bin/env bash
# https://stackoverflow.com/a/78932988/11255791
alias nvimdiff='nvim -d'
nvimgitdiff() {
if [[ "$#" == 2 ]]; then
local ref=${1}
local gitrelfp=${2}
gitfullfp=$(git ls-files --full-name $gitrelfp)
fname=$(basename ${gitrelfp})
tmpfname=/tmp/$(sed "s/\//-/g" <<< $ref)-$fname
git show $ref:$gitfullfp > $tmpfname
nvimdiff $tmpfname $gitrelfp -c "setlocal nomodifiable" # RO ref buffer
else
echo "usage: nvimgitdiff <ref|branch|commit> <relative-file-path>"
fi
}
# example usage:
# nvimgitdiff feat-branch file.yaml
# nvimgitdiff origin/master file.yaml
# nvimgitdiff 91a89847a9 file.yaml
# nvimgitdiff HEAD~3 file.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment