Get rid of null character in vim variable?
Solution 1:
Some alternatives:
Remove the
\n
usingsubstitute(string, '\n$', '', '')
See:h NL-used-for-Nul
for the technical backgroundRemove all control chars:
substitute(string, '[[:cntrl:]]', '', 'g')
Compare using the match (
=~
) operation instead of equal (==
)-
Strip away the last byte of the output from the system() command
:let a=system('foobar')[:-2]
Solution 2:
Vim represents newlines in strings as a null character, so what's happening is that your system command is returning a string with a trailing newline (as it should) and Vim is converting it to a null. You just need to remove that newline (represented as a null) at the end:
let platform_name = substitute(system('get_platform'), '\n\+$', '', '')
(Note that if you use double quotes instead of single quotes, you will have to add additional backslashes in front of the existing backslashes to escape them.)
Note that the reason \n
is used in the pattern is the same reason I explained above; Vim's representation of newlines in strings is a null.
Solution 3:
Like the others said, vim represents the newline as a null. Another way to remove the newline is in your shell command. Any one of the following would work.
let platform_name = system('echo -n "$(get_platform)"')
let platform_name = system('printf "%s" "$(get_platform)"')
let platform_name = system("get_platform | tr -d '\\n'")