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

Set variable to shell command output

Expand Messages
  • Aarto Matti
    Hi, I need to set the output from the following command to a variable: date +%s It s a string of 10 digits. I tried let stamp = execute !date + %s but
    Message 1 of 4 , Nov 30, 2009
    • 0 Attachment
      Hi,

      I need to set the output from the following command to a variable:
      date +%s
      It's a string of 10 digits.

      I tried
      let stamp = execute "!date +\%s"
      but apparently "execute" doesn't return anything, besides I can't escape "%" character which is treated as buffer name.

      --
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
    • sc
      ... just wondering: would you still want to do that if you knew vim supports its own strftime function? ... will give you a nice 11 character string in td ,
      Message 2 of 4 , Nov 30, 2009
      • 0 Attachment
        On Monday 30 November 2009 11:38:26 am Aarto Matti wrote:

        > Hi,
        >
        > I need to set the output from the following command to a
        > variable: date +%s
        > It's a string of 10 digits.
        >
        > I tried
        > let stamp = execute "!date +\%s"
        > but apparently "execute" doesn't return anything, besides I
        > can't escape "%" character which is treated as buffer name.
        >

        just wondering: would you still want to do that if you knew vim
        supports its own strftime function?

        :let td = strftime("%Y-%b-%d")

        will give you a nice 11 character string in 'td', and you can
        adjust to your preferences

        why go out for milk when there's plenty in the fridge?

        sc

        --
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
      • Gary Johnson
        ... As sc wrote, strftime() is a better solution in this case. However, for commands for which Vim does not have an internal equivalent, use system(), e.g.,
        Message 3 of 4 , Nov 30, 2009
        • 0 Attachment
          On 2009-11-30, Aarto Matti wrote:
          > Hi,
          >
          > I need to set the output from the following command to a variable:
          > date +%s
          > It's a string of 10 digits.
          >
          > I tried
          > let stamp = execute "!date +\%s"
          > but apparently "execute" doesn't return anything, besides I can't escape "%"
          > character which is treated as buffer name.

          As sc wrote, strftime() is a better solution in this case. However,
          for commands for which Vim does not have an internal equivalent, use
          system(), e.g.,

          let stamp = system("date +%s")

          That will include the trailing newline in your stamp variable. If
          that's undesirable, use this instead:

          let stamp = substitute(system("date +%s"), '\n', '', '')

          HTH,
          Gary


          --
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
        • Aarto Matti
          ... I didn t know about that function, though I was interested in general solution as well. ... This is it. Thank you all a lot! -- You received this message
          Message 4 of 4 , Dec 1, 2009
          • 0 Attachment
            On Mon, Nov 30, 2009 at 7:56 PM, sc <toothpik@...> wrote:
            just wondering:  would you still want to do that if you knew vim
            supports its own strftime function?

               :let td = strftime("%Y-%b-%d")

            will give you a nice 11 character string in 'td', and you can
            adjust to your preferences

            I didn't know about that function, though I was interested in general solution as well.

            On Mon, Nov 30, 2009 at 9:05 PM, Gary Johnson <garyjohn@...> wrote:
            As sc wrote, strftime() is a better solution in this case.  However,
            for commands for which Vim does not have an internal equivalent, use
            system(), e.g.,

               let stamp = system("date +%s")

            That will include the trailing newline in your stamp variable.  If
            that's undesirable, use this instead:

               let stamp = substitute(system("date +%s"), '\n', '', '')


            This is it.

            Thank you all a lot!

            --
            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.