OT: Vim->Ruby and back?

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

:slight_smile:

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.

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

Evening.nice!
mcc

:slight_smile:

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

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

路路路

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.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk

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.
>
> this.possible? && if true
> anser = how.ask(self)
> end
>
> Evening.nice!
> mcc
>
> :slight_smile:
>
>
>
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

Thanks a lot for that trick, Reid ! :slight_smile:

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

路路路

From: Reid Thompson <reid.thompson@ateb.com>
Subject: Re: OT: Vim->Ruby and back ?
Date: Fri, 28 Apr 2006 03:05:21 +0900

map <C-R> :!ruby %<CR>

路路路

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.

[...]
} > 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 ! :slight_smile:
}
} 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

路路路

On Fri, Apr 28, 2006 at 11:29:35AM +0900, Meino Christian Cramer wrote:

sorry... did not fully read your post. I don't know how to load it
into another buffer without saving it. :-/

路路路

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>

> 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 ?

路路路

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

路路路

On Fri, Apr 28, 2006 at 07:50:27AM -0400, Gregory Seidman wrote:

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

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.

路路路

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 ?

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

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>

路路路

On Fri, Apr 28, 2006 at 12:55:02PM +0900, Gregory Brown wrote:

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. :frowning:

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.

--
Mauricio Fernandez - http://eigenclass.org - singular Ruby