Linux – Get rid of null character in vim variable

linuxvim

I'm trying to fix vim's Backspace behavior on one of the platforms I use. I got the name of this platform (call it bad_platform) from the system's get_platform command. Following the advice in :help fixdelete, and combining it with knowledge of how to execute system commands, I added the following to my .vimrc:

let platform_name = system("get_platform")
if platform_name == "bad_platform"
  set t_kb=^?
  fixdel
endif

This didn't work. To find out why, I opened a Vim session and did :echom platform_name. That gave the result bad_platform^@, where ^@ is I guess the NULL character, not literally the two characters you get from typing "shift-6 shift-2".

Unfortunately, this presents a problem. I can't change it to == "bad_platform^@", because when the .vimrc is sourced it seems that ^@ is interpreted as an end-of-line character. This means that adding let platform_name = substitute(platform_name,"\^@","","") before the if doesn't work either.

How can I deal with this? Thanks in advance.

Best Answer

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]

Related Question