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

Receiving ouput of shell command in vimscript howto

Expand Messages
  • Shaw Vrana
    Hi All, I d like to hold the output of a shell command in a local variable. I m trying to do something like this: let myfile = silent exec !find . -name
    Message 1 of 7 , Mar 2, 2005
    • 0 Attachment
      Hi All,

      I'd like to hold the output of a shell command in a local variable. I'm
      trying to do something like this:

      let myfile = silent exec '!find . -name' myfile

      Here I'm interested in the output of find, and not the return value of find.
      (Though I would also be curious to know if I can determine the return value
      as well, though this isn't essential- I'm sure I'll be wondering how to do
      that soon enough. ;)

      :help suggestions greatly appreciated.

      Thanks,
      Shaw
    • Tim Chase
      ... My first take on the matter would be to 0) create a new window/buffer 1) use 0r! find ... (or sil! exec !find... ) 1.5) optionally save a and
      Message 2 of 7 , Mar 2, 2005
      • 0 Attachment
        > let myfile = silent exec '!find . -name' myfile

        My first take on the matter would be to

        0) create a new window/buffer
        1) use "0r! find ..." (or "sil! exec '!find...'")
        1.5) optionally save "a" and unnamed registers
        2) use ":%d a"
        3) ":let myfile = @a"
        3.5) optionally restore the "a" and unnamed registers
        4) force the closing of the new win/buffer, discarding the changes.

        At this point, you should be back in your previous buffer with
        the results of the exec() call stored in the "myfile" variable.
        It may have a trailing empty line you might have to remove. This
        might be preventable if you use "r! find..." followed by "2,$d a"
        instead. I don't know if Vim has any limits on the size of
        contents that can be shoved into a variable, which may be another
        caveat in this.

        The whole lot can be crammed into a mapping or function if you
        prefer :)

        HTH,

        -tim
      • A. J. Mechelynck
        ... You can do it on Unix Vim, see :help backtick-expansion ; but on Windows it s harder (there I guess you d have to use :r ! to read the output of the
        Message 3 of 7 , Mar 2, 2005
        • 0 Attachment
          Shaw Vrana wrote:
          > Hi All,
          >
          > I'd like to hold the output of a shell command in a local variable. I'm
          > trying to do something like this:
          >
          > let myfile = silent exec '!find . -name' myfile
          >
          > Here I'm interested in the output of find, and not the return value of find.
          > (Though I would also be curious to know if I can determine the return value
          > as well, though this isn't essential- I'm sure I'll be wondering how to do
          > that soon enough. ;)
          >
          > :help suggestions greatly appreciated.
          >
          > Thanks,
          > Shaw
          >
          >
          >
          >
          You can do it on Unix Vim, see ":help backtick-expansion"; but on
          Windows it's harder (there I guess you'd have to use ":r !" to read the
          output of the command into a buffer, then yank the buffer into a
          register, from which you can either use it as a variable or ":let" it
          into one).

          HTH,
          Tony
        • Arun Easi
          let myfile=system( find . -name .myfile) ... regards, -Arun
          Message 4 of 7 , Mar 2, 2005
          • 0 Attachment
            let myfile=system('find . -name '.myfile)

            :he system
            :he v:shell_error

            regards,
            -Arun

            On Wed, 2 Mar 2005, Shaw Vrana wrote:

            > Hi All,
            >
            > I'd like to hold the output of a shell command in a local variable. I'm
            > trying to do something like this:
            >
            > let myfile = silent exec '!find . -name' myfile
            >
            > Here I'm interested in the output of find, and not the return value of find.
            > (Though I would also be curious to know if I can determine the return value
            > as well, though this isn't essential- I'm sure I'll be wondering how to do
            > that soon enough. ;)
            >
            > :help suggestions greatly appreciated.
            >
            > Thanks,
            > Shaw
            >
            >
          • Gary Johnson
            ... That s what I usually do, too, except that I also use substitute() to remove the trailing newline from the output: let myfile=substitute(system( find .
            Message 5 of 7 , Mar 2, 2005
            • 0 Attachment
              On 2005-03-02, Arun Easi <aeasi@...> wrote:

              > On Wed, 2 Mar 2005, Shaw Vrana wrote:

              > > I'd like to hold the output of a shell command in a local variable. I'm
              > > trying to do something like this:
              > >
              > > let myfile = silent exec '!find . -name' myfile
              > >
              > > Here I'm interested in the output of find, and not the return value of find.
              > > (Though I would also be curious to know if I can determine the return value
              > > as well, though this isn't essential- I'm sure I'll be wondering how to do
              > > that soon enough. ;)
              > >
              > > :help suggestions greatly appreciated.

              > let myfile=system('find . -name '.myfile)

              That's what I usually do, too, except that I also use substitute()
              to remove the trailing newline from the output:

              let myfile=substitute(system('find . -name '.myfile), "\n", "", "")

              :help substitute()

              HTH,
              Gary

              --
              Gary Johnson | Agilent Technologies
              garyjohn@... | Wireless Division
              | Spokane, Washington, USA
            • Shaw Vrana
              ... A bunch of helpful answers here. I had not known of the system command- this will come in very handy. Thanks you all for helping me further along the
              Message 6 of 7 , Mar 2, 2005
              • 0 Attachment
                > On 2005-03-02, Arun Easi <aeasi@...> wrote:
                > let myfile=substitute(system('find . -name '.myfile), "\n", "", "")

                A bunch of helpful answers here. I had not known of the 'system' command-
                this will come in very handy. Thanks you all for helping me further along
                the learning curve!

                The system() call is mentioned in chapter 27 of Qualine's good Vim book, but I
                apparently cannot read. :(

                Thanks,
                Shaw
              • jamessan@jamessan.com
                ... That may (and probably does) have functions that aren t listed in the Vim book. James -- GPG Key: 1024D/61326D40 2003-09-02 James Vega
                Message 7 of 7 , Mar 2, 2005
                • 0 Attachment
                  On Wed, Mar 02, 2005 at 11:46:41AM -0800, Shaw Vrana wrote:
                  > > On 2005-03-02, Arun Easi <aeasi@...> wrote:
                  > > let myfile=substitute(system('find . -name '.myfile), "\n", "", "")
                  >
                  > A bunch of helpful answers here. I had not known of the 'system' command-
                  > this will come in very handy. Thanks you all for helping me further along
                  > the learning curve!
                  >
                  > The system() call is mentioned in chapter 27 of Qualine's good Vim book, but I
                  > apparently cannot read. :(

                  You can also access an alphabetical list of functions via:

                  :help functions

                  or grouped by what they're used for:

                  :help function-list

                  That may (and probably does) have functions that aren't listed in the
                  Vim book.

                  James

                  --
                  GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                Your message has been successfully submitted and would be delivered to recipients shortly.