rudolph9
rudolph9

Reputation: 8119

Open Vim from a Rakefile?

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".

Upvotes: 5

Views: 909

Answers (2)

localhostdotdev
localhostdotdev

Reputation: 1895

you need to pass the tty as standard input for backspaces etc. to work well in vim:

exec("</dev/tty vim a b")

obviously the backtick (`) didn't work but I was having issues with system/exec from a script.

first I get Vim: Warning: Input is not from a terminal, and then I see ^? when I use backspace.

Upvotes: 0

Andrew Marshall
Andrew Marshall

Reputation: 97004

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.

Upvotes: 10

Related Questions