A tiny Vim plugin for interactive Git rebasing

One of Git’s killer features is interactive rebase. After choosing a range of commits and rebasing them interactively with

git rebase -i HEAD~10

you can re-order, edit, drop and squash commits, simply by fiddling with the list of commits pasted into your favorite editor. Some actions are easily done in Vim (e.g. deleting or moving lines), others require writing whatever you intend to do with the commit. Because I squash a lot, I became tired of typing ^cws over and over again and finally sat down to write this stupid little filetype plugin:

function RebaseActionToggle()
    let line = getline(".")
    let result = matchstr(line, "^\\a")
    let transitions = {'p': 'squash', 's': 'edit', 'e': 'fixup', 'f': 'pick'}
    execute "normal! ^cw" . transitions[result]
endfunction

noremap <Cr> :call RebaseActionToggle()<Cr>

If you save it as gitrebase.vim in .vim/after/ftplugin, you can cycle through the different rebase actions by pressing Enter:

Have fun with that.


Discussion

Mike

Very nice indeed, thank you. The only thing I didn’t like was that the cursor would end up at the end of the action instead of the start of the line. That’s easy enough to fix though - just another execute statement that moves the cursor to the start of the line:

function RebaseActionToggle() 
    let line = getline(".") 
    let result = matchstr(line, "^\a") 
    let transitions = {'p': 'squash', 's': 'edit', 'e': 'fixup', 'f': 'pick'} 
    execute "normal! ^cw" . transitions[result] 
    execute "normal! ^"
endfunction

noremap  :call RebaseActionToggle()
Matthias

Glad that you like it. I also added your change to my .vimrc. Makes rebasing an even more pleasant experience ;-)