I'd like to create a bash script to automate a sequence of Git actions I do very frequently to apply my unstaged changes to an earlier commit, as in:

  • Stash changes;
  • Enter interactive rebase;
  • Edit the commit in question;
  • Apply the stash to it;
  • Finalize the rebase.

Problem is, some of these actions open up Vim and I'm not sure how to edit/write to file using a bash script. I don't want to do this manually.

Is there a way to automate editing in Vim using bash? Is there a better way to do this that covers cases where Vim isn't the default editor?

  • 2
    I understand that the question is about automating the vim part of your workflow, but I think you'll get better responses on solving this problem over at StackOverflow. That said, I did find a related link on this problem: blog.filippo.io/git-fixup-amending-an-older-commit
    – Haxiel
    Commented Nov 9, 2018 at 12:53
  • @Haxiel thanks, that's a MUCH simpler solution. Commented Nov 9, 2018 at 12:56

1 Answer 1


I guess VIM always opens up when git expects you to type in a descriptive message. You can circumvent this by providing the message to the relevant git command via the "-m" option.

e.g. see: git stash --help

git stash store [-m|--message <message>] [-q|--quiet] <commit>

additional info after clarification in comments:

There is a non-interactive (the original ex) mode in VIM: "vim -e", running the command "ex" from bash should also work. But you still would need to pipe the vim commands into it. Abusing the $EDITOR env just before your git rebase call should do it, e.g. (the -s is for silent mode):

export EDITOR="cat commands.vim | vim -es"

But when you already abuse the $EDITOR env you could also just write another bash script that does the file modifications and use that as editor:

export EDITOR="/path/to/your/script/"

Anyway, don't forget to reset your $EDITOR after that.

  • Nope, as soon as you enter interactive rebase you're given a text file with a legend to control what you want to do to the commits. I also avoid using -m cause it only gives me control over one line. Commented Nov 9, 2018 at 10:09
  • I'm not sure about what you are trying to achive. Since you are starting a interactive rebase, you have to choose somehow which commits to pick, etc. So what exactly do you want to automate?
    – swb
    Commented Nov 9, 2018 at 10:36
  • I want to automate it using the commit hash so I already know which one I want to edit... That's what I want to automate. But for that, I need to edit/write the interactive rebase menu file in Vim. Which is why I want to know if there's a way to control Vim using a bash script. Commented Nov 9, 2018 at 12:00
  • please check my addon to the original answer
    – swb
    Commented Nov 9, 2018 at 12:37
  • Thanks for the edit. A friend gave me a similar solution and I do think this is the way to go. Commented Nov 9, 2018 at 12:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .