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

Re: putting visually selected text into a string

Expand Messages
  • A.Politz
    ... Use the range mod for functions. func RunProgram() range ... let command = join(getline(a:firstline,a:lastline), n ) .... endfun ... -ap
    Message 1 of 9 , Dec 1, 2007
    View Source
    • 0 Attachment
      Vincent wrote:

      >I have the following line in my vimrc file
      >
      >map <buffer> <F3> :call RunProgram()<CR>
      >
      >In the RunProgram function i have
      >
      >let command = getline(".")
      >execute "!osascript -e 'tell application \"R\" to cmd \"" .command.
      >"\"'"
      >
      >The osascript stuff allows me on my mac to send R code to an R
      >terminal.
      >
      >The issue: if I visually select a number of lines and press F3 the
      >RunProgram function is run once for each line and I have to press <CR>
      >to get every line of code send to R. Very annoying if you are trying
      >to send 50 lines :(
      >
      >What I would like to do is have the selected lines put into the string
      >'command' and have the execute line applied to that string once.
      >Problem is i don't know how to do that.
      >
      >Any suggestions?
      >
      >Thanks,
      >
      >Vincent
      >>
      >
      >
      Use the range mod for functions.

      func RunProgram() range
      ...
      let command = join(getline(a:firstline,a:lastline),"\n")
      ....
      endfun

      :h a:lastline
      :h function-range-example

      -ap

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Vincent
      sc, I might have a buffer with the following 2 lines: x
      Message 2 of 9 , Dec 1, 2007
      View Source
      • 0 Attachment
        sc,

        I might have a buffer with the following 2 lines:

        x <- rnorm(100)
        plot(x)

        I used A.Politz suggestion (thanks!) and it almost works. The issue is
        that i get an error when i select both lines -- E492: Not an editor
        command: plot(x)"'

        R will accept code segments separated by at ';' but ideally it would
        be a return after each line of R code.

        Best,

        Vincent

        On Nov 30, 11:42 pm, sc <tooth...@...> wrote:
        > On Friday 30 November 2007 23:33, Vincent wrote:
        >
        >
        >
        > > I have the following line in my vimrc file
        >
        > > map <buffer> <F3> :call RunProgram()<CR>
        >
        > > In the RunProgram function i have
        >
        > > let command = getline(".")
        > > execute "!osascript -e 'tell application \"R\" to cmd \""
        > > .command. "\"'"
        >
        > > The osascript stuff allows me on my mac to send R code to
        > > an R terminal.
        >
        > > The issue: if I visually select a number of lines and press
        > > F3 the RunProgram function is run once for each line and I
        > > have to press <CR> to get every line of code send to R.
        > > Very annoying if you are trying to send 50 lines :(
        >
        > > What I would like to do is have the selected lines put into
        > > the string 'command' and have the execute line applied to
        > > that string once. Problem is i don't know how to do that.
        >
        > > Any suggestions?
        >
        > i'll ask the question everyone with answers is wondering:
        > what exactly do you want between what was the selected lines
        > of the buffer as they are passed to osascript?
        >
        > sc
        >
        > > Thanks,
        >
        > > Vincent
        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Tony Mechelynck
        ... You can yank the selected text into a register, for example y to yank to the default ( quote ) register, or wy to yank it to a named register (here,
        Message 3 of 9 , Dec 1, 2007
        View Source
        • 0 Attachment
          Vincent wrote:
          > I have the following line in my vimrc file
          >
          > map <buffer> <F3> :call RunProgram()<CR>
          >
          > In the RunProgram function i have
          >
          > let command = getline(".")
          > execute "!osascript -e 'tell application \"R\" to cmd \"" .command.
          > "\"'"
          >
          > The osascript stuff allows me on my mac to send R code to an R
          > terminal.
          >
          > The issue: if I visually select a number of lines and press F3 the
          > RunProgram function is run once for each line and I have to press <CR>
          > to get every line of code send to R. Very annoying if you are trying
          > to send 50 lines :(
          >
          > What I would like to do is have the selected lines put into the string
          > 'command' and have the execute line applied to that string once.
          > Problem is i don't know how to do that.
          >
          > Any suggestions?
          >
          > Thanks,
          >
          > Vincent

          You can yank the selected text into a register, for example

          y

          to yank to the default ("quote") register, or

          "wy

          to yank it to a "named" register (here, register w -- there are 26 such, from
          a to z).

          Then you can move your register to a variable if you want to, or you can even
          use it as a variable (the variable name is @" or @@ for the default register,
          @w for register w, etc.):

          y
          :let command = @@

          or

          "wy
          :let command = @w

          or even

          "cy

          then later use @c as a variable name in any expression.


          Best regards,
          Tony.
          --
          hundred-and-one symptoms of being an internet addict:
          151. You find yourself engaged to someone you've never actually met,
          except through e-mail.

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • A.Politz
          ... Try to use system() instead of exec and ! . -ap -- Ich hab geträumt, der Krieg wär vorbei. --~--~---------~--~----~------------~-------~--~----~ You
          Message 4 of 9 , Dec 1, 2007
          View Source
          • 0 Attachment
            Vincent wrote:

            >sc,
            >
            >I might have a buffer with the following 2 lines:
            >
            >x <- rnorm(100)
            >plot(x)
            >
            >I used A.Politz suggestion (thanks!) and it almost works. The issue is
            >that i get an error when i select both lines -- E492: Not an editor
            >command: plot(x)"'
            >
            >R will accept code segments separated by at ';' but ideally it would
            >be a return after each line of R code.
            >
            >Best,
            >
            >Vincent
            >
            >...
            >
            >>>execute "!osascript -e 'tell application \"R\" to cmd \""
            >>>.command. "\"'"
            >>>
            >....
            >
            >

            Try to use system() instead of exec and '!'.

            -ap



            --
            Ich hab geträumt, der Krieg wär vorbei.


            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Vincent
            Thanks for the suggestions! The following works exactly as i had wanted. function! RunRProgram() range let command =
            Message 5 of 9 , Dec 1, 2007
            View Source
            • 0 Attachment
              Thanks for the suggestions!

              The following works exactly as i had wanted.

              function! RunRProgram() range
              let command = join(getline(a:firstline,a:lastline),"\\n")
              execute "!osascript -e 'tell application \"R\" to cmd
              \"" .command. "\"'"
              endfunction

              ap: I tried using system but can't seem to get that to work.

              system("osascript -e 'tell application \"R\" to cmd \"" .command.
              "\"'")


              Vincent


              On Dec 1, 4:43 pm, "A.Politz" <poli...@...> wrote:
              > Vincent wrote:
              > >sc,
              >
              > >I might have a buffer with the following 2 lines:
              >
              > >x <- rnorm(100)
              > >plot(x)
              >
              > >I used A.Politz suggestion (thanks!) and it almost works. The issue is
              > >that i get an error when i select both lines -- E492: Not an editor
              > >command: plot(x)"'
              >
              > >R will accept code segments separated by at ';' but ideally it would
              > >be a return after each line of R code.
              >
              > >Best,
              >
              > >Vincent
              >
              > >...
              >
              > >>>execute "!osascript -e 'tell application \"R\" to cmd \""
              > >>>.command. "\"'"
              >
              > >....
              >
              > Try to use system() instead of exec and '!'.
              >
              > -ap
              >
              > --
              > Ich hab geträumt, der Krieg wär vorbei.
              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Ben Schmidt
              ... You would need to :call system(...), if you didn t. System is a function so needs to live inside an expression, and the :call command is a nice command
              Message 6 of 9 , Dec 2, 2007
              View Source
              • 0 Attachment
                > system("osascript -e 'tell application \"R\" to cmd \"" .command.
                > "\"'")

                You would need to :call system(...), if you didn't. System is a function so needs
                to live inside an expression, and the :call command is a nice command that just
                executes an expression and discards the result (unlike :exe which evaluates the
                expression then executes the result as a command).

                But hey, you have already solved your problem, so no need!

                Ben.





                Send instant messages to your online friends http://au.messenger.yahoo.com


                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Vincent
                This is actually even nicer. Thanks for your help Ben! Best, Vincent ... --~--~---------~--~----~------------~-------~--~----~ You received this message from
                Message 7 of 9 , Dec 2, 2007
                View Source
                • 0 Attachment
                  This is actually even nicer. Thanks for your help Ben!

                  Best,

                  Vincent

                  On Dec 2, 4:34 am, Ben Schmidt <mail_ben_schm...@...> wrote:
                  > > system("osascript -e 'tell application \"R\" to cmd \"" .command.
                  > > "\"'")
                  >
                  > You would need to :call system(...), if you didn't. System is a function so needs
                  > to live inside an expression, and the :call command is a nice command that just
                  > executes an expression and discards the result (unlike :exe which evaluates the
                  > expression then executes the result as a command).
                  >
                  > But hey, you have already solved your problem, so no need!
                  >
                  > Ben.
                  >
                  > Send instant messages to your online friendshttp://au.messenger.yahoo.com
                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                Your message has been successfully submitted and would be delivered to recipients shortly.