Use shlex to split custom vim command

This allows to include spaces in vim command
Add example to change default diff algorithm
Move to first line on each file
This commit is contained in:
Balakrishnan Balasubramanian 2022-12-20 13:57:14 -05:00
parent bb6fa28de0
commit da5e24260a
2 changed files with 15 additions and 12 deletions

View File

@ -3,6 +3,7 @@ Do you use `git difftool` to review changes before making a commit? The problem
# Install # Install
```bash
mkdir -p ~/bin mkdir -p ~/bin
# for python version >= 3.10 # for python version >= 3.10
@ -12,6 +13,7 @@ Do you use `git difftool` to review changes before making a commit? The problem
curl -o ~/bin/vimtabdiff.py "https://raw.githubusercontent.com/balki/vimtabdiff/py38/vimtabdiff.py" curl -o ~/bin/vimtabdiff.py "https://raw.githubusercontent.com/balki/vimtabdiff/py38/vimtabdiff.py"
chmod +x ~/bin/vimtabdiff.py chmod +x ~/bin/vimtabdiff.py
```
You may need to add `~/bin` to your PATH variable if not already done. See [here](https://wiki.archlinux.org/title/Environment_variables#Per_user) for help. You may need to add `~/bin` to your PATH variable if not already done. See [here](https://wiki.archlinux.org/title/Environment_variables#Per_user) for help.
👍 this [issue](https://github.com/balki/vimtabdiff/issues/1) for `pip install` support 👍 this [issue](https://github.com/balki/vimtabdiff/issues/1) for `pip install` support
@ -69,6 +71,7 @@ You may need to add `~/bin` to your PATH variable if not already done. See [here
## Using custom vim command ## Using custom vim command
Using clean vim without reading `vimrc`
```bash ```bash
git config --global difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "vim --clean" $LOCAL $REMOTE' git config --global difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "vim --clean" $LOCAL $REMOTE'
``` ```
@ -82,16 +85,14 @@ Git config file (`~/.gitconfig`) should look like this
[difftool "vimtabdiff"] [difftool "vimtabdiff"]
cmd = vimtabdiff.py --vim \"vim --clean\" $LOCAL $REMOTE cmd = vimtabdiff.py --vim \"vim --clean\" $LOCAL $REMOTE
``` ```
Using better diff algorithm
```bash
git config --global difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "vim +\"set diffopt+=algorithm:patience\"" $LOCAL $REMOTE'
# Known issues ```
1. If your path to vim has a space, it does not work, i.e. Following does **not** work *Note:* Not tested in non-linux OS. But I guess it should work fine. Pull requests welcome if found any issues.
```bash
git config difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "/home/foo/my program files/bin/vim" $LOCAL $REMOTE'
```
2. Not tested in non-linux OS. Pull requests welcome if found any issues but hopefully should work fine.
# Similar # Similar

View File

@ -9,6 +9,7 @@ import argparse
import itertools import itertools
import tempfile import tempfile
import subprocess import subprocess
import shlex
from pathlib import Path from pathlib import Path
from typing import Callable, TypeVar from typing import Callable, TypeVar
from collections.abc import Iterator, Sequence from collections.abc import Iterator, Sequence
@ -23,8 +24,8 @@ def star(f: Callable[..., T]) -> Callable[[Sequence], T]:
def parse_args() -> argparse.Namespace: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Show diff of files from two directories in vim tabs", description="Show diff of files from two directories in vim tabs",
epilog="See https://github.com/balki/vimtabdiff for more info") epilog="See https://github.com/balki/vimtabdiff for more info")
parser.add_argument("pathA", type=Path) parser.add_argument("pathA", type=Path)
parser.add_argument("pathB", type=Path) parser.add_argument("pathB", type=Path)
parser.add_argument("--vim", help="vim command to run", default="vim") parser.add_argument("--vim", help="vim command to run", default="vim")
@ -77,14 +78,15 @@ def main() -> None:
aPath = a.resolve() if a else os.devnull aPath = a.resolve() if a else os.devnull
bPath = b.resolve() if b else os.devnull bPath = b.resolve() if b else os.devnull
print( print(
f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis | diffupdate", f"tabedit {aPath} | vsp {bPath} | windo diffthis | windo diffupdate",
file=vimCmdFile) file=vimCmdFile)
cmds = f""" cmds = f"""
tabdo windo :1
tabfirst | tabclose tabfirst | tabclose
call delete("{vimCmdFile.name}") call delete("{vimCmdFile.name}")
""" """
print(cmds, file=vimCmdFile) print(cmds, file=vimCmdFile)
subprocess.run(args.vim.split() + ["-S", vimCmdFile.name]) subprocess.run(shlex.split(args.vim) + ["-S", vimCmdFile.name])
if __name__ == '__main__': if __name__ == '__main__':