Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

vim - Beginner scripting: ^@ appearing at the end of text

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

^@ 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, '
', '', 'g')

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...