2

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.

4
  • What do you mean? What do you want on the right side and what on the left?
    – svick
    Apr 30, 2011 at 21:31
  • Looks like a vim support question - perhaps belongs on superuser?
    – bdonlan
    Apr 30, 2011 at 21:37
  • 1
    vim questions belong here per a vote on meta. Apr 30, 2011 at 22:02
  • That’s not about commiting in vim, but in shell. What i want is : when i do a commit in the bash shell with the command «git commit -v», instead of open one window in vim with my commit message and the diff in comments, i want to open vim with two windows : on the left one, my commit message and on the right one, my diff. So i can read diff and message in the same time.
    – Ryzz
    May 1, 2011 at 15:53

3 Answers 3

3

I usually do this:

:new<CR>:r!git diff --cached<CR>:setf diff

Use :vnew instead of :new to get a vertical split.

1

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.
0

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"
2
  • Nice trick ! just use vim instead of vi in the alias command !
    – Ryzz
    Jun 3, 2011 at 13:07
  • Other stuff : I had to put a ! after "normal" to make it work and I put a ^wh at the end of the GIT_EDITOR string in order to be in the commit window, not in the diff one (assumed that I use "set splitright" in my vimrc to split on right). I tried to have a better trick by putting the alias in the gitconfig: civ= !GIT_EDITOR="vim -c 'normal! gg/^diff^MdGgg:vnew^MP:set ft=diff^M^Wh'" git commit -v but it didnt’ work.
    – Ryzz
    Jun 3, 2011 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.