Forum: Ruby OT: Vim->Ruby and back ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-04-27 19:58
(Received via mailing list)
Hi,

 sorry for this offtopic mail, but I asked this on the vim-user
 mailing list and git no answer...

 What I wnat to do seems to be simple: I want to write a script
 with vim and -- without saveing it -- pass it to the ruby interpreter
 and get back the output including stderr AND stdout back into a new
 buffer.

 this.possible? && if true
	anser = how.ask(self)
 end

 Evening.nice!
 mcc

 :)
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2006-04-27 20:08
(Received via mailing list)
Meino Christian Cramer wrote:
>  this.possible? && if true
> 	anser = how.ask(self)
>  end
>
>  Evening.nice!
>  mcc
>
>  :)
>
>
>
I pulled this from the web, or the mailing list, somewhere..... a google
on it should point you to the correct author...

in .gvimrc/.vimrc add

map <C-I> :!rm -f /tmp/*put<C-M> :w /tmp/input<C-M> :!/bin/ruby
/tmp/input > /tmp/output 2>&1<C-M> :vs +e! /tmp/output<C-M><C-W>l

map <C-L> <C-W>o


short form of descriptions
<CTRL-I> execute and paste results in left side of vertical split
<CTRL-L><CTRL-W>  close left side of vertical split
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-04-28 04:30
(Received via mailing list)
From: Reid Thompson <reid.thompson@ateb.com>
Subject: Re: OT: Vim->Ruby and back ?
Date: Fri, 28 Apr 2006 03:05:21 +0900

> >
> >
>
> short form of descriptions
> <CTRL-I> execute and paste results in left side of vertical split
> <CTRL-L><CTRL-W>  close left side of vertical split
>

Thanks a lot for that trick, Reid ! :)

But this only write the buffer, start ruby, exute that file with ruby
with ridirected output to another file and reads back that file into
vim. Additionally it does not check, whether both files already
exist...thex will be overwritten simply.

I wanted to exeute ruby with the buffer contents without writing to
the disk at all for that reason.

Is this possible ?

Ruby!
mcc
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-28 05:36
(Received via mailing list)
On 4/27/06, Meino Christian Cramer <Meino.Cramer@gmx.de> wrote:

> I wanted to exeute ruby with the buffer contents without writing to
> the disk at all for that reason.

map <C-R> :!ruby %<CR>
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-28 05:42
(Received via mailing list)
On 4/27/06, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 4/27/06, Meino Christian Cramer <Meino.Cramer@gmx.de> wrote:
>
> > I wanted to exeute ruby with the buffer contents without writing to
> > the disk at all for that reason.
>
> map <C-R> :!ruby %<CR>
>

sorry... did not fully read your post.  I don't know how to load it
into another buffer without saving it. :-/
Cdb3ec23d3ee6366dcffe49150a0a9e9?d=identicon&s=25 Meino Christian Cramer (Guest)
on 2006-04-28 05:48
(Received via mailing list)
From: "Gregory Brown" <gregory.t.brown@gmail.com>
Subject: Re: OT: Vim->Ruby and back ?
Date: Fri, 28 Apr 2006 12:35:32 +0900

> On 4/27/06, Meino Christian Cramer <Meino.Cramer@gmx.de> wrote:
>
> > I wanted to exeute ruby with the buffer contents without writing to
> > the disk at all for that reason.
>
> map <C-R> :!ruby %<CR>

I inserted the above mapping into $HOME/.vimrc .>

In a buffer I wrote:

#!/usr/bin/ruby
print "hallo"


and pressed <Ctrl-R>


An error pops up saying:

E499: Empty file name for '%' or '#', only works with ":p:h"

(dont know before, that the error message are in Chinese ... ;O)

And now ?
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-28 05:57
(Received via mailing list)
On 4/27/06, Meino Christian Cramer <Meino.Cramer@gmx.de> wrote:

> E499: Empty file name for '%' or '#', only works with ":p:h"
>
> (dont know before, that the error message are in Chinese ... ;O)
>
> And now ?

hmm... looks like it needs to be run against a saved file.  :(

I hope that someone finds a solution to this, because I would like to
add somethign to my .vimrc that can do what you want.
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 Pit Capitain (Guest)
on 2006-04-28 09:05
(Received via mailing list)
Gregory Brown schrieb:
> I hope that someone finds a solution to this, because I would like to
> add somethign to my .vimrc that can do what you want.

How about

   :vmap <C-R> y:exe "ruby " . getreg()<CR>

Mark some text in visual mode and press <C-R> to send it to the builtin
Ruby interpreter. Note that the interpreter isn't restarted upon each
invocation, so previous definitions stay valid, which may or may not be
what you need.

Regards,
Pit
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-04-28 09:38
(Received via mailing list)
On Fri, 2006-04-28 at 02:56 +0900, Meino Christian Cramer wrote:

>  What I wnat to do seems to be simple: I want to write a script
>  with vim and -- without saveing it -- pass it to the ruby interpreter
>  and get back the output including stderr AND stdout back into a new
>  buffer.

I've wanted this myself, but never quite got there.

* You can use :w !ruby to run the current buffer without saving it and
  see the output.
* You can use :r !ruby % to run the current buffer AFTER saving it and
  and get it's output to the same buffer.
* You can use :e to open new buffers

I've never found a good way to bring them together into a nice round
trip shortcut, and unfortunately I don't know if they'll suit your needs
anyway since :r still writes out a temporary file...
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-28 13:53
(Received via mailing list)
On Fri, Apr 28, 2006 at 11:29:35AM +0900, Meino Christian Cramer wrote:
[...]
} > Meino Christian Cramer wrote:
} > > Hi,
} > >
} > >  sorry for this offtopic mail, but I asked this on the vim-user
} > >  mailing list and git no answer...
} > >
} > >  What I wnat to do seems to be simple: I want to write a script
} > >  with vim and -- without saveing it -- pass it to the ruby
interpreter
} > >  and get back the output including stderr AND stdout back into a
new
} > >  buffer.
[...]
}
} Thanks a lot for that trick, Reid ! :)
}
} But this only write the buffer, start ruby, exute that file with ruby
} with ridirected output to another file and reads back that file into
} vim. Additionally it does not check, whether both files already
} exist...thex will be overwritten simply.
}
} I wanted to exeute ruby with the buffer contents without writing to
} the disk at all for that reason.
}
} Is this possible ?

I can get you stderr and stdout mised together, not in separate files:

0G"ryG:new
"rp:%!ruby

This yanks the entire buffer into register r, opens a new window, pastes
register r into the window, feeds the entire buffer into ruby's stdin
and
replaces the buffer with what the script writes to stdout or stderr.
Unfortunately, I don't know how to distinguish between output to stdout
and
stderr. Since you are writing the script, however, you should know which
is
which.

} Ruby!
} mcc
--Greg
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-28 14:33
(Received via mailing list)
On Fri, Apr 28, 2006 at 07:50:27AM -0400, Gregory Seidman wrote:
[...]
} I can get you stderr and stdout mised together, not in separate files:
}
} 0G"ryG:new
} "rp:%!ruby

Sorry to respond to my own post, but this is a much nicer-looking (and
tested!) version of the above:

:%y r|new|pu r|%!ruby

} This yanks the entire buffer into register r, opens a new window,
pastes
} register r into the window, feeds the entire buffer into ruby's stdin
and
} replaces the buffer with what the script writes to stdout or stderr.
} Unfortunately, I don't know how to distinguish between output to
stdout and
} stderr. Since you are writing the script, however, you should know
which is
} which.
}
} } Ruby!
} } mcc
} --Greg
--Greg
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2006-04-28 16:48
(Received via mailing list)
On Fri, Apr 28, 2006 at 12:55:02PM +0900, Gregory Brown wrote:
> I hope that someone finds a solution to this, because I would like to
> add somethign to my .vimrc that can do what you want.

Here's one idea + cheap implementation:
* F7 in visual mode evaluates the selected area; temporary files are
used so
  the original one isn't overwritten. The output (stdout & stderr) is
shown in
  a new vsplit window (on the right)
* F7 in normal and insert modes evaluate the whole buffer; the cursor
position
  is preserved (warning: overwrites the 'z' mark)
* S-F7 kills the stdout/stderr buffer; it is also removed from the
  minibufexplorer


function! Ruby_eval_vsplit() range
    let src = tempname()
    let dst = tempname()
    execute ": " . a:firstline . "," . a:lastline . "w " . src
    execute ":silent ! ruby " . src . " > " . dst . " 2>&1 "
    execute ":redraw!"
    execute ":vsplit"
    execute "normal \<C-W>l"
    execute ":e! " . dst
    execute "normal \<C-W>h"
endfunction

vmap <silent> <F7> :call Ruby_eval_vsplit()<cr>
nmap <silent> <F7> mzggVG<F7>`z
imap <silent> <F7> <ESC><F7>a
map <silent> <S-F7> <C-W>l:bw<cr>
imap <silent> <S-F7> <ESC><S-F7>
This topic is locked and can not be replied to.