views:

156

answers:

2

I have these Vim mappings for Git. Most of the commands below work, but the last one, gm, doesn't work at all. I want to open a new Vim (preferrably with my git commit message template already loaded, as per the default terminal behavior), edit and save my message, then commit.

Any ideas for another way to approach this?

" git shortcuts
"" show diff in new window
if has("gui_running")
  noremap gd :!git diff <BAR> gvim -<CR><CR>
else
  noremap gd :!git diff <BAR> vim -<CR><CR>
endif
noremap gs :!git status<CR>
noremap ga :!git add %<CR>
"" edit commit message in new window
if has("gui_running")
  noremap gm :!gvim __vim_gitcommitmessage && git commit -F __vim_gitcommitmessage && rm __vim_gitcommitmessage<CR>
else
  noremap gm :!vim __vim_gitcommitmessage && git commit -F __vim_gitcommitmessage && rm __vim_gitcommitmessage<CR>
endif

Update:

As per VonC's suggestion, I am now using fugitive.vim. I replaced my previous Vim bindings, and would recommend this plugin to anyone who uses Vim and Git.

" fugitive shortcuts
noremap ggs :Gstatus<cr>
noremap ggc :Gcommit<cr>
noremap gga :Gwrite<cr>
noremap ggl :Glog<cr>
noremap ggd :Gdiff<cr>
A: 

What exactly is the point of editing a file then telling git to use it as the commit message? Why can't you just use the normal mode of operation - run git commit, let it spawn vim, write the message, save and quit to commit? Or if you need a template, git commit -e -F <template file>.

Jefromi
Thanks for the response, but I think you misunderstand me. I want a new Vim for the commit message, i.e. I'm editing a file and then I hit `ga` to add it and `gm` to commit the change, which spawns a new Vim to edit the commit message. Ideally, upon `:wq` of the commit message, a `git commit` is performed. So I'm never really taken away from the file that I'm editing, and can do all of my Git operations without leaving my editor.
sidewaysmilk
So spawn `git commit` in the background in a new terminal, or set your editor to gvim so you don't need a terminal. Your original vim will return, and the new vim will be the one you edit the commit message in. The commit will be performed on `:wq` of the message in the editor which `git commit` spawns - this is the default mode of operation of `git commit`.
Jefromi
I'm talking about issuing a command in Vim to do a Git commit without leaving the editor. This doesn't work the way that you describe it. My `EDITOR` is Vim. The default behavior from the terminal opens Vim with a commit template. However, within Vim, I just get a broken pipe and commit fails because there is no message. Dropping back to the terminal does work--it's what I do now--but the point of this question is that I want to stop doing that. It's the **one thing** that forces me to leave Vim while I'm editing.
sidewaysmilk
first comment: "new vim for the commit message" second comment: "do a git commit without leaving the editor" I'm confused, but you accepted VonC's answer so... I guess it's okay.
Jefromi
Those two goals are completely compatible. I don't think you understand what I'm trying to do. Anyway, VonC's answer does exactly what I want. I'll update the question with my implementation.
sidewaysmilk
@sidewaysmilk: so you wanted to spawn a new *window*, not a new instance of Vim, I guess? That does make sense, and does make my answer useless! I didn't notice it saying "window" in the question title - and you just said "a new Vim" in the question body, and in your comment, so I read it as a full instance of vim. Sorry about all that.
Jefromi
I was being loose with the terms "window" and "Vim." I did prefer a new window (in the GUI window manager sense) over a new Vim window (buffer). So a new instance of Vim was what I had in mind. But the fugitive.vim interface actually makes buffers work really well.Again, thanks for trying.
sidewaysmilk
+3  A: 

Vim has some Git wrapper scripts which encapsulates common Git commands, and allows committing through a temporary buffer:

One that could fit the bill would be fugitive.vim: see its doc:

:MinSCMCommitTracked[!]         (Default mapping: \s<C-c>)

Opens temporary buffer for you to enter commit message. Write the commit buffer and all tracked files are committed.

            Used SCM commands ~
            hg  : commit
            git : commit -a
            bzr : commit

:MinSCMCommitAll[!]             (Default mapping: \sc)

Opens temporary buffer for you to enter commit message. Write the commit buffer and all files in a working directory of a current repository are committed.

This command is different from |:MinSCMCommitTracked| in adding untracked files to the current repository.

            Used SCM commands ~
            hg  : commit -A
            git : add -a && commit -a
            bzr : add && commit

The source being available, you could reuse the part you need in your own Vim script.

VonC
This isn't ''exactly'' what I was looking for (one more plugin to keep around and learn and use >_<) but it's probably better than what I had in mind. THANK YOU!
sidewaysmilk
I looked back at your answer and notice that your sample code is actually for MinSCM and not fugitive.vim. Still, thanks for fugitive.vim. :)
sidewaysmilk