Get rid of null character in vim variable?

7

2

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.

Sam Rabin

Posted 2015-07-02T16:46:01.707

Reputation: 173

Answers

8

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]

Christian Brabandt

Posted 2015-07-02T16:46:01.707

Reputation: 1 196

1I can confirm that all of these work. Thanks! – Sam Rabin – 2015-07-06T16:40:21.227

1

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

dosentmatter

Posted 2015-07-02T16:46:01.707

Reputation: 133

1

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.

Heptite

Posted 2015-07-02T16:46:01.707

Reputation: 16 267

This didn't work for me; the syntax presented in @Christian 's #1 did though. – Sam Rabin – 2015-07-06T16:36:07.077

You're right, the \'s needed to be escaped with a \, or the double quotes should have been single. Fixed my answer. – Heptite – 2015-07-06T17:39:13.940