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's what I have so far:
function! CreateShebang()
call inputsave()
let program = input('Enter the program name: ')
call inputrestore()
let path = system('which ' . program)
call append(0, '#!' . path)
endfunction开发者_如何学Python
By the way, I'm just starting with vim scrips, so if you notice any wrong function and concepts or know a better way to achieve the result, please tell me. Any help is really appreciated.
The big problem is that after running, the scripts prompts for the program name correctly and then add something like this to the file:
#!/usr/bin/perl^@
What's that ^@
doing there?
Also, If I may ask another question here, how can I clear the command line after input()
? The text entered by the user keeps showing until another command is entered.
^@
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.
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
精彩评论