Last Updated: November 02, 2017
·
2.228K
· shakeelmohamed

Changing My Git Editor On The Fly

I love using Sublime Text as do many developers these days, but I just can't use it regularly for git commit messages.

When I learned git 4 years ago I always used the default editor - vim. I don't particularly enjoy using vim, but it's the command-line editor I know best. For the longest time I would write inline commit messages, ie: git commit -m "hello world".

Then one day something crazy happened...

CapturFiles_2.png

Since then, about 1 year ago, my default commit workflow has become the following

  • git commit
  • (vim launches)
  • type i
  • write awesome commit message
  • type esc + :wq

Nothing special about it, but this workflow is so engrained in my muscle memory I can't seem to turn it off. So... what's the problem? Rebasing commits. Let's say my goal is to squash 10 commits - including merges, this involves editing several lines at once in the same way.

In the main Zen Audio Player repo, I ran git rebase -i HEAD~10. Here's the workflow I'd need for either editor.

In vim

CapturFiles.png

  • j to go down a line, skip the first commit
  • xxxx to delete the 4 characters in pick
  • i to start inserting text
  • s (squash) or f (fixup)
  • esc
  • Repeat the previous 4 steps many times. Actually 48 more times since 50 commits are involved in this rebase!

Estimated completion time: 5 minutes

Probability of making a mistake: 50%

In sublime

CapturFiles_1.png

  • Press the down key
  • Use cmd + d to select the 49 other several instances of the word pick
  • type s (squash) or f (fixup) to change all instances
  • cmd + s to save

Estimated completion time: 15 seconds

Probability of making a mistake: 1%

So, what's the solution?

When in doubt, create a bash alias or function!
Since these are the only 2 editors, I've hardcoded 2 bash functions that will instantly change my git editor.

Switch to sublime

gitsubl() {
    git config --global core.editor "subl -n -w"
}

Switch to vim

gitvim() {
    git config --global core.editor "vim"
}

Now, whenever I need to do some rebasing:

  • run gitsubl
  • rebase away...
  • run gitvim to go back to normal

1 Response
Add your response

If vim is your default editor having a vimrc with at least set nu for line numbers is helpful. Secondly Ctrl-V to start block selection, <Line Number>G to jump to that line number, e to go to the end of the word, s to delete the word and start editing, type squash/fixup, hit ESC. Will do exactly what you're doing in Sublime. 10 seconds.

Ctrl-V,20G,es,"squash",ESC

over 1 year ago ·