Loading ...
Sorry, an error occurred while loading the content.

Call external command with stdin

Expand Messages
  • Ovid
    Hi all, I am trying to write an extract method function like this: vmap e :call ExtractSub() function!
    Message 1 of 2 , Dec 5, 2011
    • 0 Attachment
      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/

      --
      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
    • Ben Fritz
      ... Like what, now? Can you repost with linebreaks and indent? ... Yes, because in visual mode, implicitly Vim adds a range of
      Message 2 of 2 , Dec 5, 2011
      • 0 Attachment
        On Dec 5, 9:31 am, Ovid <publiustemp-...@...> 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
      Your message has been successfully submitted and would be delivered to recipients shortly.