Monday, December 5, 2011

Call external command with stdin

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.

Originally I had this:

   vmap <leader>e :!~/bin/extract <cr>
 
That worked fine, but I read the selected code from STDIN. I can't figure out how to do that with a function, 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?

Cheers,
Ovid
--
Live and work overseas - http://overseas-exile.blogspot.com/
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://blogs.perl.org/users/ovid/
Twitter - http://twitter.com/OvidPerl/

No comments: