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

Trimming newlines from calls to system()

Expand Messages
  • Ben Klein
    ... I get a ^@ (null) at the end of the result: testing^@ If I instead use echo, which doesn t translate unprintable characters but ... I get the result of the
    Message 1 of 7 , Feb 14, 2014
      When I echom something from the command line using system():

          :echom system("echo \"testing\"")

      I get a ^@ (null) at the end of the result:

          testing^@

      If I instead use echo, which doesn’t translate unprintable characters but displays them:

          :echo system("echo \"testing\")

      I get the result of the command line command with an added newline:

          testing
          

      This of course normally makes sense at the command line, but when I’m returning output taken from the command line as a Vim message I don’t want an unnecessary newline. I can eliminate it by returning the first item in a List made from split() used on the return value from system() with "\n" as the pattern (or perhaps by substitute-ing newlines with ''), but is there a simpler way to do this?

      Thanks!

      Ben

      --
      b

      --
      --
      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
       
      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Jean-Rene David
      ... You don t say what your operating system is, but ... I almost never use echo in the shell. Too much of a pain. -- JR -- -- You received this message from
      Message 2 of 7 , Feb 14, 2014
        * Ben Klein [2014.02.14 14:00]:
        > When I echom something from the command line using system():
        >     :echom system("echo \"testing\"")
        > I get a ^@ (null) at the end of the result:
        >     testing^@
        > If I instead use echo, which doesn’t translate unprintable characters but
        > displays them:
        >     :echo system("echo \"testing\")
        > I get the result of the command line command with an added newline:
        >     testing

        You don't say what your operating system is, but
        in a posix shell you could do:

        :echo system("printf \"testing\"")

        I almost never use echo in the shell. Too much of
        a pain.

        --
        JR

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

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • ZyX
        ... Usually if you know for sure that command will output trailing newline (which you do in something like 90% of cases) you just use `system()[:-2]`.
        Message 3 of 7 , Feb 14, 2014
          On Friday, February 14, 2014 11:00:30 PM UTC+4, Benjamin Klein wrote:
          > When I echom something from the command line using system():
          >
          >     :echom system("echo \"testing\"")
          >
          > I get a ^@ (null) at the end of the result:
          >
          >     testing^@
          > If I instead use echo, which doesn’t translate unprintable characters but displays them:
          >
          >     :echo system("echo \"testing\")
          >
          > I get the result of the command line command with an added newline:
          >
          >     testing
          >
          > This of course normally makes sense at the command line, but when I’m returning output taken from the command line as a Vim message I don’t want an unnecessary newline. I can eliminate it by returning the first item in a List made from split() used on the return value from system() with "\n" as the pattern (or perhaps by substitute-ing newlines with ''), but is there a simpler way to do this?

          Usually if you know for sure that command will output trailing newline (which you do in something like 90% of cases) you just use `system()[:-2]`. `substitute(system(), "\n*$", '', '')` if you want to trim any amount of newlines (like `str.rstrip('\n')` in python).

          >
          > Thanks!

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

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Ben Klein
          On Fri, Feb 14, 2014 at 2:17 PM, ZyX wrote: Usually if you know for sure that command will output trailing newline ... Thanks. Am I
          Message 4 of 7 , Feb 14, 2014
            On Fri, Feb 14, 2014 at 2:17 PM, ZyX <zyx.vim@...> wrote:

            Usually if you know for sure that command will output trailing newline (which you do in something like 90% of cases) you just use `system()[:-2]`. `substitute(system(), "\n*$", '', '')` if you want to trim any amount of newlines (like `str.rstrip('\n')` in python).

             Thanks. Am I correct, however, in thinking that the first method assumes Python support in the Vim instance? (See http://vim.1045645.n5.nabble.com/String-manipulation-in-vim-scripts-tp1169327p1169330.html.)

            Ben

            --
            b

            --
            --
            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
             
            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • ZyX
            ... VimL slices have nothing to do with python. If they had it would be referenced in documentation. The only person referencing Python in the discussion you
            Message 5 of 7 , Feb 14, 2014
              >  Thanks. Am I correct, however, in thinking that the first method assumes Python support in the Vim instance? (See http://vim.1045645.n5.nabble.com/String-manipulation-in-vim-scripts-tp1169327p1169330.html.)

              VimL slices have nothing to do with python. If they had it would be referenced in documentation.

              The only person referencing Python in the discussion you provided link to is saying nonsense. Python support does not make vim able to do define variables without :let. Slice assignment is not supported by python strings. Python support does not make vim able to perform slice assignment on string variables. scott-268 managed to have one sentence with three lies in it.

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

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Benjamin Klein
              ... Wow. I *did* wonder about that. Many thanks for clearing that up. :-) Ben -- b Sent from my iPhone -- -- You received this message from the vim_use
              Message 6 of 7 , Feb 14, 2014
                On Feb 14, 2014, at 3:31 PM, ZyX <zyx.vim@...> wrote:

                > VimL slices have nothing to do with python. If they had it would be referenced in documentation.
                >
                > The only person referencing Python in the discussion you provided link to is saying nonsense. Python support does not make vim able to do define variables without :let. Slice assignment is not supported by python strings. Python support does not make vim able to perform slice assignment on string variables. scott-268 managed to have one sentence with three lies in it.

                Wow. I *did* wonder about that. Many thanks for clearing that up. :-)

                Ben

                --
                b

                Sent from my iPhone

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

                ---
                You received this message because you are subscribed to the Google Groups "vim_use" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.
              • Benjamin Klein
                ... Sorry. I am on OS X, and could indeed have used printf. echo was just an example of a command from the figurative top of my head -- it s not really
                Message 7 of 7 , Feb 14, 2014
                  On Feb 14, 2014, at 1:33 PM, Jean-Rene David <vim_use@...> wrote:

                  > You don't say what your operating system is, but
                  > in a posix shell you could do:
                  >
                  > :echo system("printf \"testing\"")
                  >
                  > I almost never use echo in the shell. Too much of
                  > a pain.

                  Sorry. I am on OS X, and could indeed have used printf. echo was just an example of a command from the figurative top of my head -- it's not really relevant to the discussion; I just wanted an example of command line output. :-)

                  Ben

                  --
                  b

                  Sent from my iPhone

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

                  ---
                  You received this message because you are subscribed to the Google Groups "vim_use" group.
                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                  For more options, visit https://groups.google.com/groups/opt_out.
                Your message has been successfully submitted and would be delivered to recipients shortly.