Get rid of null character in vim variable?

Solution 1:

Some alternatives:

  1. Remove the \n using substitute(string, '\n$', '', '') See :h NL-used-for-Nul for the technical background

  2. Remove all control chars: substitute(string, '[[:cntrl:]]', '', 'g')

  3. Compare using the match (=~) operation instead of equal (==)

  4. 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'")