开发者

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:

  1. insert setlocal previewheight=0 at the top of the function to make both windows equal in size.
  2. Change the line beginning with exe "pedit "... to exe "vert bot pedit "... to have the preview open on the right.
  3. 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

  1. Go the the top of the file
  2. Go the first diff hunk
  3. Cut all the diff hunks (until the end of the file)
  4. Create a new window vertically on the left of the current window (see 'splitright' option if you don't like this position)
  5. Paste the diff hunks
  6. 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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜