10

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.

3 Answers 3

12

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]

1
  • 1
    I can confirm that all of these work. Thanks!
    – Sam R
    Jul 6, 2015 at 16:40
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.

2
  • This didn't work for me; the syntax presented in @Christian 's #1 did though.
    – Sam R
    Jul 6, 2015 at 16:36
  • You're right, the \\'s needed to be escaped with a \\, or the double quotes should have been single. Fixed my answer.
    – Heptite
    Jul 6, 2015 at 17:39
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'")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .