6

A few months back I switched from Textmate to Vim. Overall I really love Vim, but one of the things I miss from Textmate is using the ⌘R command to run Ruby code and having the results neatly pop up in a new, scrollable window. Obviously, Vim is capable of running Ruby code and displaying the output with :w !ruby. The only downside to this is that if the resulting output is too long I can't scroll through it.

To combat this problem I tried modifying a :redir function from Vim Tips. It looks like this:

function! TabMessage(cmd)
  redir => message
  silent execute a:cmd
  redir END
  tabnew
  silent put=message
  set nomodified
endfunction
command! -nargs=+ -complete=command TabMessage call TabMessage(<q-args>)

Now the output from Ruby is put into a new tab. However, I can't get it to pop up in a new, separate window. Changing tabnew to new just sends the output to a split in the same window.

The other problem is that a visible ^M gets appended to the end of each line, so the output ends up looking like this, which is kind of bothersome:

Hello World!^M

So, is there any way to get the output into a separate window without the ^M appended to the end? Are there any plugins I should be using in order to achieve this Textmate-like effect for code output?

2 Answers 2

1

It sounds like you're using MacVim, so try this:

:w !ruby | mvim -c "set nomodified" -

Assuming you have the mvim script somewhere in your path, that will execute the contents of the current buffer, then pipe the output into an instance of MacVim running in a new OS X window (vs. a new Vim window). The new buffer is marked unmodified to avoid warnings when you try to close it without saving.

More info:

:help macvim
:help mvim
2
  • that definitely worked. i'll add it to my vimrc. it also got rid of the funky line endings in the evaluated code. thanks very much. Apr 23, 2010 at 19:31
  • You're welcome. Glad to help.
    – Bill Odom
    Apr 23, 2010 at 19:36
1

A couple of suggestions:

  1. Why is :!ruby foo.rb not working for you? Assuming your vim is running in a scrollable window? Or how about :!ruby foo.rb > foo.out, followed by :e foo.out After that you'll have 2 buffers open, the original file you were working in & the results from running ruby. You can do all sorts of things with buffers (open the buffers in a split window, etc). Check out :help :buffer for more options in this area.
  2. If vim was compiled with ruby support (+ruby) you can use ruby commands in vim, such as :ruby {cmd}. Check out :help :ruby for more info on this.
  3. You may be better served installing rails.vim and possibly some other vim packages. The Biodegradeable Geek has a nice writeup on using VIM as a Ruby on Rails IDE
1
  • 1. As an example try evaluating a file containing (1..500).each {|x| puts x} using :!ruby. The window will display about the last 50 numbers and cannot be scrolled through. Your buffer suggestions is also fine, but essentially the same as the tab function I'm using now. 2. I know about this and use it often. 3. I'll check out the article and see if I can glean any useful information, but I don't really use Ruby on Rails. Apr 23, 2010 at 13:49

You must log in to answer this question.

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