Beginner scripting: ^@ appearing at the end of text

后端 未结 2 477
广开言路
广开言路 2020-12-19 19:10

I\'m trying to create a script that helps creating shebangs (Ok, it may not be that useful but has advantages when you don\'t know where the program is, for example), here\'

相关标签:
2条回答
  • 2020-12-19 19:25

    Probably the which command output contains the NULL character.

    The system() function replaces line breaks with <NL>s. (from :help system()). Therefore you could do:

    let path = substitute(system('which ' . program), '\%x00', '', 'g')
    

    Otherwise you could do the following:

    function! CreateShebang()
        call inputsave()
        0 put = '#!/usr/bin/env ' . input('Enter the program name: ')
        call inputrestore()
    endfunction
    
    0 讨论(0)
  • 2020-12-19 19:44

    ^@ at the end of command is a newline translated to NULL by append() function, see :h NL-used-for-Nul (it the reason why your substitute(...\%d000...) worked while you don't have NULL in your string). As which command always outputs newline at the end of string, I suggest you to slightly modify your code by adding [:-2] to the end of the system() call. This construction will strip just the last byte of function output:

    let path = system('which ' . program)[:-2]
    

    If you use substitute, use

    let path=substitute(path, '\n', '', 'g')
    

    , don't confuse yourself with \%d000 which is semantically wrong.

    0 讨论(0)
提交回复
热议问题