Initial scripting: ^ @ appears at the end of the text

I am trying to create a script that helps to create shebangs (well, maybe this is not so useful, but it has advantages when you do not know where the program is, for example), thatโ€™s what I still have

function! CreateShebang()
    call inputsave()
    let program = input('Enter the program name: ')
    call inputrestore()
    let path = system('which ' . program)
    call append(0, '#!' . path)
endfunction

By the way, Iโ€™m just starting with vim scripts, so if you notice any wrong functions and concepts or know the best way to achieve the result, please tell me. Any help really appreciated.

The big problem is that after starting the scripts ask for the program name correctly, and then add something like this to the file:

#!/usr/bin/perl^@

What is doing there ^@?

, , input()? , , , .

+3
2

^@ - , NULL append(), . :h NL-used-for-Nul ( , substitute(...\%d000...) , NULL ). which , , [:-2] system(). :

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

,

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

\%d000, .

+4

, which NULL.

system() <NL> s. ( :help system()). :

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

:

function! CreateShebang()
    call inputsave()
    0 put = '#!/usr/bin/env ' . input('Enter the program name: ')
    call inputrestore()
endfunction
+1

Source: https://habr.com/ru/post/1794445/


All Articles