How to have the result of «git commit -v» in a splited window instead of in comments ?
From git help commit : « -v, --verbose 开发者_运维技巧 Show unified diff between the HEAD commit and what would be committed at the bottom of the commit message template. Note that this diff output doesn’t have its lines prefixed with #. »
That’s wonderful but i’d prefer having it in a vertical splited window.
I usually do this:
:new<CR>:r!git diff --cached<CR>:setf diff
Use :vnew instead of :new to get a vertical split.
There's a vim command :DiffGitCached
in $VIMRUNTIME/ftplugin/gitcommit.vim
. It uses the preview window to show the diff. You can make a local copy (~/.vim/ftplugin/gitcommit.vim
) and modify the gitdiffcached(...)
function slightly:
- insert
setlocal previewheight=0
at the top of the function to make both windows equal in size. - Change the line beginning with
exe "pedit "...
toexe "vert bot pedit "...
to have the preview open on the right. - insert
wincmd p
at the end of the function if you want the focus to move back to the commit-msg window by default.
This should do what you want:
$ GIT_EDITOR="vim -c 'normal gg/^diff^MdGgg:vnew^MP:set ft=diff^M'"
$ export GIT_EDITOR
$ git commit -v
The "^M" is the enter key (carriage return), entered by pressing Ctrl+V and then pressing enter.
The argument to the "-c" option tells vim to
- Go the the top of the file
- Go the first diff hunk
- Cut all the diff hunks (until the end of the file)
- Create a new window vertically on the left of the current window (see 'splitright' option if you don't like this position)
- Paste the diff hunks
- Set the file type to diff to get the nice highlighting (you do have "syntax on" in your .vimrc, do you?)
Obviously you want this command to be invoked only when you run "git commit -v" and nowhere else because the vim command sequences applies only in that case so it might not be a good idea to set GIT_EDITOR as such.
A better configuration is use alias:
$ alias gcv="GIT_EDITOR=\"vi -c 'normal gg/^diff^MdGgg:vnew^MP:set ft=diff^M'\" git commit -v"
精彩评论