Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Git commit message in new Vim window, then commit all within Vim

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>
like image 700
Justin Force Avatar asked Apr 09 '26 13:04

Justin Force


2 Answers

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.

like image 57
VonC Avatar answered Apr 11 '26 03:04

VonC


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

like image 40
Cascabel Avatar answered Apr 11 '26 03:04

Cascabel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!