Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start typing when entering VIM during git rebase? [closed]

While using Powershell 5, when I type git rebase -i <sha> vim fires up and I get the a list of commits on their pick lines.

Moving the caret to the start of any of those lines and changing pick to squash, s, fixup, f etc appears to be blocked initially. It would appear that the cursor keys should be hit randomly, random characters entered and eventually vim lets text be entered as I intend.

What is vim doing here and is it simply an option? How do you stop it?

[EDIT] I realise that the initial mode of vim may not be the mode I'm looking for, however I'm not sure how to check that, change it or anything related. If the initial mode of vim, as entered when typing git rebase... does need changing, a good answer to this question would be how to change that mode once vim has started up.

like image 960
Matt W Avatar asked Aug 07 '17 13:08

Matt W


People also ask

How do you go into interactive mode in rebase?

You can run rebase interactively by adding the -i option to git rebase . You must indicate how far back you want to rewrite commits by telling the command which commit to rebase onto. Remember again that this is a rebasing command — every commit in the range HEAD~3..

What should I do after git rebase?

When you do a Git rebase you take that latest state of the master branch. Then commit by commit your changes are re-added on top of the latest state on master. After each commit is replayed there is a check if there are conflicts which you should then fix, but more on that later.

How do I get out of git rebase interactive?

Press Esc , :wq! and Enter to save and exit. Now, rebase will stop at Added Mary commit.


2 Answers

I've been through what you're describing and it can be really confusing! I'm hoping this helps, but please reply via comment if anything is unclear and I'll be happy to respond or update my answer :)

In Vim, you start out in normal mode.

Ready to edit

From there, you can access insert mode by pressing i and can edit the text as you'd normally expect. When done editing text, press esc to return to normal mode

Save changes

In normal mode, type :wq and press Enter to 'Write' and 'Quit' out of Vim.

Horrible Mistake! Need to exit without changes!

To avoid applying your changes in Vim, you'd normally use :q! (quit without saving, forced) in normal mode, but that won't work for interactive rebasing in the way you'd expect. Instead, you must delete all lines from the text before using :wq or :q!. An easy way to delete all lines at once in normal mode would be to follow the answer selected from This Question

I want to Squash or Fixup so many lines...

Vim is an amazing text editor and allows you to do so many things! One of my favorites in the context of interactive rebasing is text replacement: in normal mode, type :%s/pick/fixup/g and all instances of "pick" will be replaced with "fixup". You can then enter insert mode to make minor changes to the first commit (change to edit, pick, reword, etc) and any other commit lines as needed.

like image 188
Jonathan Avatar answered Sep 28 '22 08:09

Jonathan


By default vim sessions start out awaiting commands (rather than text to be inserted at the caret). This is typical behavior across vi implementations.

If you position the caret using the arrow keys or mouse (as opposed to the traditional h, j, k, and l commands) then you might find that making vim start in insert mode works for you. You can do this in several ways. One way would be to change the editor command, by doing this:

git config --global core.editor 'vi +star'

Of course you'll still have to exit the "insert" mode when you're done, in order to then enter the "save and exit" commands. (You exit "insert" mode by hitting the esc key.)

That said, you may find that it's more convenient to let vim start in the default mode, because then you can do things like: position the caret on the pick that you want to change; type c w (the "change word" command); type squash (or whatever you want to change the pick to); hit the esc key to get back out of insert mode. Hit ZZ to save and close vim.

(Basically, if you invest the time to really learn how to work with vi-type editors, then the default mode will start making more sense; and if you don't want to invest that time, you might want to just switch away from using vim.)

like image 32
Mark Adelsberger Avatar answered Sep 28 '22 08:09

Mark Adelsberger