59

I happily use vim as my default editor for commits, and do not wish to change it. However, when it comes to rebasing, I find myself squashing dozens and dozens of commits which I find much easier with an interactive editor like Textwrangler (substituting "pick" with "squash" in all but the top commit).

Is there any way to specify an alternate editor for a one-off rebase command?

I know in vim I can do:

:%s/pick/squash/

but that has its own minor annoyances.

EDIT - as stated in the comments, you can squash all but the top commit very efficiently by going to the 2nd line and executing

:,$s/pick/squash/

(note the comma and dollar are different to the original)

Sridhar Sarnobat
  • 22,210
  • 12
  • 81
  • 103
  • 5
    In vim, you can do `:,$s/pick/squash/` which will change all occurrences from the *current line* through to the end of the file. – Greg Hewgill Oct 31 '13 at 18:35
  • That's a great tip, thanks Greg. Yes, that was the "minor annoyance" I was alluding to. – Sridhar Sarnobat Oct 31 '13 at 18:36
  • Assuming you have an up to date vim version or have install Tim Pope's vim-git plugin, you can use the `:Cycle` or `:Squash` commands. You may even want to use a mapping. Maybe use the `:global` command: `:+,$g/./Squash` – Peter Rincker Oct 31 '13 at 18:37
  • May I ask what :Cycle and :Squash do? I'm having trouble finding the documentation for those commands. – Sridhar Sarnobat Oct 31 '13 at 18:39
  • I also use "marks" heavily. Go to the first line you want to change, type `ma` (set mark `a`), go to the last you want to change, type `:'a,.s/this/that/`. Single quote followed by a mark-letter means "the line where that mark is set". – torek Oct 31 '13 at 19:18
  • `:Cycle` will cycle through the rebase options: `pick`, `squash`, `edit`, `rework`, and `fixup`. `:Squash` will mark a line as `squash`. Take a look at `e $VIMRUNTIME/ftplugin/gitrebase.vim` for more information. – Peter Rincker Oct 31 '13 at 20:08
  • Thanks for the info Peter, and torek. – Sridhar Sarnobat Oct 31 '13 at 20:14

2 Answers2

71

Try adding the GIT_EDITOR environment variable before your command, like so:

GIT_EDITOR=<editor of choice> git rebase <...>

For example, to use nano I would type:

GIT_EDITOR=nano git rebase -i abcdef1234
Rob Bajorek
  • 5,962
  • 7
  • 44
  • 49
  • Since you aren't exporting it, does that variable value get reset when the command terminates? (that would be ideal, but I'm guessing you need to reset it manually) – Sridhar Sarnobat Oct 31 '13 at 18:37
  • 1
    The variable is only set for that one command invocation. Try running `git rebase` again without `GIT_EDITOR` and you'll use your default editor again. – Rob Bajorek Oct 31 '13 at 18:43
  • Awesome, this is the right answer then. Though I think my fears of learning a bit more vim were irrational – Sridhar Sarnobat Oct 31 '13 at 19:14
  • 1
    Awesome indeed. For some reason my ancient server-side OS defaulted to `vi` instead of `vim` --so now I know how to perma-fix this. – MarkHu May 06 '16 at 17:31
  • This helped me a lot thanks, i believe a lot of people are looking for `GIT_EDITOR=nano git rebase -i @~9` this will show your last nine commits and you should be able to modify them. – Bobby Axe May 28 '20 at 16:14
  • For visual code use GIT_EDITOR="code --wait" git rebase -i abfa452 – EddyXorb Mar 16 '21 at 06:55
18

There is an even better option for all your interactive rebase.

https://github.com/sjurba/rebase-editor

It is a custom CLI app written in node specifically for interactive rebase.

To install:

npm install -g rebase-editor
git config --global sequence.editor rebase-editor 

Or with yarn:

yarn global add rebase-editor
git config --global sequence.editor rebase-editor 
Tim
  • 7,107
  • 2
  • 47
  • 83
barsju
  • 4,408
  • 1
  • 18
  • 24