初学者脚本:^@ 出现在文本末尾 [英] Beginner scripting: ^@ appearing at the end of text
问题描述
我正在尝试创建一个有助于创建 shebangs 的脚本(好吧,它可能没有那么有用,但在您不知道程序在哪里时具有优势,例如),这是我目前所拥有的:
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
顺便说一句,我刚开始使用vim脚本,所以如果您发现任何错误的功能和概念或知道实现结果的更好方法,请告诉我.任何帮助都非常感谢.
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^@
^@
在那里做什么?
另外,如果我可以在这里问另一个问题,如何在 input()
之后清除命令行?用户输入的文本会一直显示,直到输入另一个命令.
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.
推荐答案
^@
在命令的末尾是一个由 append()
函数转换为 NULL 的换行符,见 :h NL-used-for-Nul
(这就是为什么你的 substitute(...\%d000...)
在你没有 NULL 的情况下工作的原因在你的字符串中).由于 which
命令总是在字符串的末尾输出换行符,我建议您通过将 [:-2]
添加到 系统的末尾来稍微修改您的代码()
调用.这种结构将只去除函数输出的最后一个字节:
^@
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')
,不要将自己与语义错误的 \%d000 混淆.
, don't confuse yourself with \%d000 which is semantically wrong.
这篇关于初学者脚本:^@ 出现在文本末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!