Friday, February 14, 2014

Re: Trimming newlines from calls to system()

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@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: