How do I use vim as 'git log' editor?

2019-04-06 14:34发布

问题:

When I run git log, what exactly is the editor git log is using?

Also:

  1. Is there anyway I can use vim as my default editor for git log?
  2. If I want to search against the git log, what's the best way? Now I'm doing something like: git log | grep bla.

回答1:

The git log command pipes it's output by default into a pager, not an editor. This pager is usually less or more on most systems. You can change the default pager to vim with the command:

git config --global core.pager 'vim -'

Now you can search using vim functionality with / as usual.



回答2:

You may want to look at viewlog.

It gives you a terminal gui interface to all commits. It will also open the file in the editor of your choice. You can search for relevant commits using the --grep flag.



回答3:

I had write a bash script to switch from using vim as editor to diff-so-fancy.

VT() {
    gitlogflag=true
    if [ -e "$HOME/.myscriptvar" ] ; then
        gitlogflag=$(cat "$HOME/.myscriptvar")
    fi
    if [ "$gitlogflag" = true ]; then
        git config --global pager.show "vim -c '%sm/\\e.\\{-}m//g' -c 'setlocal buftype=nofile' -c 'set ft=diff' +3 -";
        gitlogflag=false
    else
        git config --global pager.show "diff-so-fancy | less --tabs=1,5 -RFX";
        gitlogflag=true
    fi
    echo "$gitlogflag" > $HOME/.myscriptvar
}

I recommend you to use diff-so-fancy.

If you just want to use vim as your git log editor git config --global pager.show "vim -c '%sm/\\e.\\{-}m//g' -c will satisfy you.