在 Vim 中,
:%!ls
执行ls
命令并将其输出打印到当前可编辑文件。
但是中%
和分别代表什么意思?!
vim
是否可以执行ls
但不将其输出放入文档?
答案1
答案2
根据VIM 教程:
%
Move cursor to the matching bracket.
Place cursor on {}[]() and type "%".
!
filter through external command
Any UNIX command can be executed from the vi command line by typing an "!" before the UNIX command.
Autowrite can be intentionally avoided by using "!" to avoid the save when switching files.
答案3
我也有同样的问题,并在中找到了这一点:h cmdline-special
,这就是我所寻找的含义:
% Is replaced with the current file name. *:_%* *c_%*
答案4
为了理解发生了什么,我们将把这个问题分为两个部分。首先,范围进而筛选。
范围是 vim 用来表示操作发生位置的方式,如下所示':帮助范围'我们可以有这些:
Line numbers may be specified with: :range E14 {address}
{number} an absolute line number
. the current line :.
$ the last line in the file :$
% equal to 1,$ (the entire file) :%
't position of mark t (lowercase) :'
'T position of mark T (uppercase); when the mark is in
another file it cannot be used in a range
/{pattern}[/] the next line where {pattern} matches :/
?{pattern}[?] the previous line where {pattern} matches :?
\/ the next line where the previously used search
pattern matches
\? the previous line where the previously used search
pattern matches
\& the next line where the previously used substitute
pattern matches
注意‘%’将对整个文件应用操作。
另一部分是过滤操作,按照':help 过滤器',过滤方式之一是:
:{range}![!]{filter} [!][arg] :range!
Filter {range} lines through the external program
{filter}. Vim replaces the optional bangs with the
latest given command and appends the optional [arg].
Vim saves the output of the filter command in a
temporary file and then reads the file into the buffer
tempfile. Vim uses the 'shellredir' option to
redirect the filter output to the temporary file.
However, if the 'shelltemp' option is off then pipes
are used when possible (on Unix).
注意命令的第一部分是告知范围。
那么,就你的情况来说,:%!ls通知 vim 执行ls命令并将结果应用于整个文件。