loading...

The Handful of Commands I Use When Interactive Rebasing with Vim

noelworden profile image Noel Worden ・3 min read

This week I expanded my VIM knowledge a bit, to make my rebasing life a little bit easier. Although VIM is not my default editing workspace, I use it for a few things, so that if I find myself in a scenario where I have to use it I'm not completely lost. I primarily use it for commit messages and rebasing, and get by efficiently enough with my basic VIM knowledge.

I've been interactive rebasing a lot lately to update commits, so I'm often finding myself with a git log that looks something like this:

477cd5f 2020-05-14 Noel Worden fix - merge into Commit A
1487595 2020-05-14 Noel Worden FAM-225: Commit B
d9bf948 2020-05-14 Noel Worden FAM-197: Commit A

Which means when I enter an interactive rebase looks like this:

pick d9bf948 FAM-197: Commit A
pick 1487595 FAM-225: Commit B
pick 477cd5f fix - merge into Commit A

Basically, I need to move 477cd5f fix - merge into Commit A up one line. The 'old', crunky way I had been doing it was to go into insert mode, add a new line, type out fixup and the SHA, then go down and hit the delete key a bunch of times to the original commit line:

pick d9bf948 FAM-197: Commit A
fixup 477cd5f << manually type this
pick 1487595 FAM-225: Commit B
pick 477cd5f fix - merge into Commit A << come back and manually delete this

It did the job, but I knew there was a better way. And, heres that better way:

jjddkP

Yup, thats it. A few taps of the keyboard does what took me a good 15 seconds or more when trying to do it all in insert mode.

Heres a breakdown:

j and k are directional keys (up/down) along with h and l (left/right); the dd and P commands are where the real action is:

jj - move down two lines
dd - delete the current line, and store in clipboard
k - move up one line
P - put (paste) from clipboard before cursor (note that it's an uppercase P)

That gets the line moved up to where it needs to be, but I'm usually rebasing to merge work into an existing commit, so I use fixup (or just f) to drop the commit message of the commit. That can be done with three key strokes:

cwf

The breakdown:

cw - change word from current cursor position
f - fixup -> like squash, but discard this commit's log message

And this is the result of those handful of commands:

pick d9bf948 FAM-197: Commit A
f 477cd5f fix - merge into Commit A
pick 1487595 FAM-225: Commit B

Which is exactly what I need, and much faster than my original 'all insert' method.

This is how I have found I best learn things like hot keys or shortcuts; instead of memorizing entire sheets of hotkeys, I introduce small chunks of shortcuts as I need them. The internet is full of VIM cheatsheets, I like this one in particular. I also find that searching the internet for my specific need can be a good way to find solutions quickly.


This post is part of an ongoing This Week I Learned series. I welcome any critique, feedback, or suggestions in the comments.

Posted on by:

noelworden profile

Noel Worden

@noelworden

Software Engineer in Boulder, CO - Writing code and getting strategically lost in the mountains

Discussion

pic
Editor guide