Monday, December 5, 2011

Re: Call external command with stdin

On Dec 5, 9:31 am, Ovid <publiustemp-...@yahoo.com> wrote:
> Hi all,
>
> I am trying to write an "extract method" function like this:
> vmap <leader>e :call ExtractSub()<cr>                                          function! ExtractSub() range " yank selection into x "normal gv"xy let code    = shellescape(join(getline(a:firstline, a:lastline),"\n")) let subname = input("Sub name: ") " put extracted sub/method into the x register let @x = system("~/bin/extract --subname=" . subname.' '.code) " re-select and delete normal gvd " paste x into visual areaq normal "xP  endfunction
> The idea is that I select a few lines of code in visual mode, pass them to my /bin/extract code and have it rewrite my code.
>

Like what, now? Can you repost with linebreaks and indent?

> Originally I had this:
>
>    vmap <leader>e :!~/bin/extract <cr>
>
>
> That worked fine, but I read the selected code from STDIN.

Yes, because in visual mode, implicitly Vim adds a range of '<,'> to
anything done on the command-line. So what Vim actually executes is:

'<,'>!~/bin/extract


> I can't figure out how to do that with a function,

You do it in the same way. It looks like you're using a:firstline and
a:lastline as the lines to work on, so should be able to just do
something like:

exe a:firstline.",".a:lastline.'!~/bin/extract'

> so I resorted to trying to pass it on the command line. That led to some garbled code (I have a bunch of \\ at the end of lines). Is there a way I can still have /bin/extract reading from STDIN in the function?
>

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: