[英]what does the number 1 in the shellescape function mean in vim?
A vim command confuses me. vim命令让我困惑。 I have read and re-read
:help shellescape()
several times. 我已阅读并重新阅读
:help shellescape()
几次。 I still don't understand the meaning of the number 1
in shellescape(expand('%:p'), 1)
. 我仍然不明白
shellescape(expand('%:p'), 1)
数字1
的shellescape(expand('%:p'), 1)
。
Here's the full command I'm trying to understand: 这是我试图理解的完整命令:
:nnoremap <F4> :exe ':silent !"c:\Program Files\Mozilla Firefox\firefox.exe"'shellescape(expand('%:p'), 1)<CR>
Let's break down this long command piece by piece. 让我们一块一块地分解这个长命令。
:exe
is to execute some command. :exe
是执行一些命令。 :silent !
is to execute a shell command silently "c:\\Program Files\\Mozilla Firefox\\firefox.exe"
can call my firefox program. "c:\\Program Files\\Mozilla Firefox\\firefox.exe"
可以调用我的firefox程序。 shellescape()
, there are blanks to be escaped. shellescape()
,有空白逃脱。 expand('%:p')
can get current file name in full expression that is path + filename. expand('%:p')
可以在完整表达式中获取当前文件名,即path + filename。 What does this excerpt from the help page mean? 从帮助页面中摘录的内容是什么意思?
With a |non-zero-arg|
使用|非零arg | {special} and 'shell' containing "csh" in the tail it's
{special}和'shell'在尾部包含“csh”
escaped a second time.第二次逃脱。
Is there some meaning such the same as 1 or 2 in s/(ha.*)(world)/\\2\\1/g
? 在
s/(ha.*)(world)/\\2\\1/g
是否有一些与1或2相同的含义?
please take a simple example in detail. 请详细举一个简单的例子。
And i have two questions related to the topic. 我有两个与该主题相关的问题。
1.In which way i can get how many type of shells in my gvim? 1.我可以通过哪种方式获得我的gvim中有多少种贝壳?
2.In which situation can i change 1
into 2
in shellescape()? 2.在哪种情况下我可以在shellescape()中将
1
改为2
?
:nnoremap <F4> :exe ':silent !"c:\Program Files\Mozilla Firefox\firefox.exe"'shellescape(expand('%:p'), 2)<CR>
There are basically two different uses for shellescape()
; shellescape()
基本上有两种不同的用途; one is the Vimscript system()
function, the other the :!
一个是Vimscript
system()
函数,另一个是:!
Ex command. 退出命令。 While most escaping needs are identical in both uses (eg to deal with whitespace, arguments must be enclosed in quotes), the
:!
虽然大多数转义需求在两种用途中都是相同的(例如,处理空格,参数必须用引号括起来), 但是
:!
command requires some additional escaping , because on the command-line, Vim assigns special meaning to symbols like %
(replacing it with the current file name). 命令需要一些额外的转义 ,因为在命令行中,Vim为
%
符号赋予特殊含义(用当前文件名替换它)。 This does not happen for system()
. system()
不会发生这种情况。
Therefore, to tell shellescape()
which escaping mode to use, it has the additional {special}
argument; 因此,要告诉
shellescape()
哪种转义模式,它有额外的{special}
参数; if you pass 1
(or any other value that evaluates to "true"), the additional characters are escaped, too. 如果传递
1
(或任何其他评估为“true”的值),则其他字符也会被转义。
TL;DR: Pass 1
for :!
TL; DR:通过
1
:!
commands, 0
or omit the argument for use with system()
. 命令,
0
或省略用于system()
的参数。
From the help for shellescape()
: 来自
shellescape()
的帮助shellescape()
:
shellescape({string} [, {special}]) shellescape()
Escape {string} for use as a shell command argument.
On MS-Windows and MS-DOS, when 'shellslash' is not set, it
will enclose {string} in double quotes and double all double
quotes within {string}.
For other systems, it will enclose {string} in single quotes
and replace all "'" with "'\''".
When the {special} argument is present and it's a non-zero
Number or a non-empty String (non-zero-arg), then special
items such as "!", "%", "#" and "<cword>" will be preceded by
a backslash. This backslash will be removed again by the :!
command.
The 1
in your example simply tells shellescape()
to escape special characters with a backslash. 您的示例中的
1
只是告诉shellescape()
使用反斜杠转义特殊字符。 If you were to have (say) a !
如果你有(说)一个
!
in the path returned by expand()
, it'd be replaced with \\!
在
expand()
返回的路径中,它将替换为\\!
. 。
shell
is an option: shell
是一个选项:
'shell' 'sh' E91
'shell' 'sh' string (default $SHELL or "sh",
MS-DOS and Win32: "command.com" or
"cmd.exe", OS/2: "cmd")
global
Name of the shell to use for ! and :! commands.
— :help 'shell'
-
:help 'shell'
“ With a non-zero-arg {special} and 'shell' containing "csh" in the tail it's escaped a second time
” means that if, as in your example, shellescape()
is given a non-zero argument for special
(your example has a 1
), it will check shell
for that "csh"
, and if it finds it (if your shell
is set to something containing csh
) it will double-escape it. “
With a non-zero-arg {special} and 'shell' containing "csh" in the tail it's escaped a second time
”意味着如果像你的例子那样, shellescape()
被赋予special
的非零参数(你的例子有一个1
),它将检查shell
的"csh"
,如果它找到它(如果你的shell
被设置为包含csh
东西),它将双重转义它。
EDIT to specifically answer two questions added to (edited) original post: 编辑专门回答添加到(编辑)原帖的两个问题:
You can get your shell
setting (referred to in the shellescape()
help quote) using :echo &shell
. 您可以使用
:echo &shell
获取shell
设置(在shellescape()
帮助引用中引用)。
2
is a Number
and is non-zero. 2
是Number
且非零。 You should therefore be able to substitute 2
for the 1
in your example and get the same result. 因此,您应该能够在示例中将
2
替换为1
并获得相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.