1

I am trying to create an alias on Ruby:

#!/usr/bin/env ruby

if ARGV[0]
  `git add -A && git commit -m #{ARGV[0]} && git push`
else
  `git add -A && EDITOR='TERM=xterm vim' git commit && git push`
end

I tried first without EDITOR..., but any thing I try I get Vim: Warning: Output is not to a terminal.

What I want is vim to appear when I do not enter any commit message. But instead I get that message.

Update

I think what I need is to capture editor output as git does. How could I do that?

sites
  • 21,417
  • 17
  • 87
  • 146

1 Answers1

2

Backticks capture command output into a string for you to use in your script, which Vim is rightfully unhappy with because it won't be able to print you a UI. Use system() instead, which hands everything over to the new process. If you don't need to come back to your Ruby script, you can use exec(), which replaces the process entirely.

You're also going to need to quote your message as git commit -m "#{ARGV[0]}", or it will be interpreted as git commit -m My Commit Message and complain there are no files Commit or Message. And then of course, you'll need to escape the message itself so you don't break out of the doublequotes early. Look at Shellwords#shellescape for that.

Kristján
  • 18,165
  • 5
  • 50
  • 62
  • 1
    For more information on the differences between `[backtick]` `system()` and `exec()`, take a look at this other SO question: http://stackoverflow.com/questions/6338908/ruby-difference-between-exec-system-and-x-or-backticks – jkeuhlen Jul 29 '15 at 15:50