假设我将此vimscript设为"/tmp/example.vim":
let g:input = "START; % END"
exec("! clear && echo " . shellescape(g:input))
Run Code Online (Sandbox Code Playgroud)
如果我打开该文件并运行它:so %
,输出将是
START; /tmp/example.vim END
Run Code Online (Sandbox Code Playgroud)
因为"%"扩展为缓冲区名称.我想要输出
START; % END
Run Code Online (Sandbox Code Playgroud)
我可以使用泛型escape()
方法来逃避百分号.这有效:
let g:input = "START; % END"
exec("! clear && echo " . escape(shellescape(g:input), "%"))
Run Code Online (Sandbox Code Playgroud)
但这真的是最好的方式吗?我敢肯定我应该逃避更多的角色.为此目的是否有特定的逃逸功能?还是一个更好的方式来掏出来?
要与:!
命令一起使用,您需要将可选{special}
参数传递给shellescape()
:
当
{special}
参数存在,这是一个非零数值或者是非空的字符串(|非零ARG |),则特殊项目如!
,%
,#
和<cword>
将由反斜杠.这个反斜杠将被|删除:!
| 命令.
:exec("! clear && echo " . shellescape(g:input, 1))
Run Code Online (Sandbox Code Playgroud)