Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Vim from a Rakefile?

Tags:

ruby

rake

I am creating a journal application for personal notes and have the following in my Rakefile:

task :new do
  entry_name = "Entries/#{Time.now.to_s.gsub(/[-\ :]+/, '.').gsub(/.0500+/,'')}.md"
  `touch #{entry_name}`
  `echo "# $(date)" >> #{entry_name}`
end

The last part I would like to include is the opening of the Vim text editor but I am unable to figure out how to open it as if I called it directly from the bash terminal.

I have tried:

vim #{entry_name}

but unfortunately I think both of those open it as a background process.

I have been referencing "6 Ways to Run Shell Commands in Ruby".

like image 685
rudolph9 Avatar asked Jun 13 '12 18:06

rudolph9


1 Answers

As in the article you referenced, `s run the command in a subshell within the current process, but the real problem is that it's trying to take the output from the command run as well, which doesn't play nice with Vim.

You can either:

  • Use exec to replace the current process with the new one (note that the Ruby/Rake process will end once you've called exec, and nothing after it will run).

  • Use system to create a subshell like `s, but avoids the problem of trying to grab Vim's stdout. Unlike exec, after Vim terminates, Ruby will continue.

like image 123
Andrew Marshall Avatar answered Oct 18 '22 12:10

Andrew Marshall