在Vim中,我可以使用以下命令回显当前文件名:
:echo @%
我在这里找到了这些信息:http://vim.wikia.com/wiki/Get_the_name_of_the_current_file
有人可以解释为什么@符号是必要的吗?如果我输入没有@符号的命令,我会收到一个错误:
E15: Invalid expression: %
E15: Invalid expression: %
但是,如果我尝试将文件名作为参数发送到bang命令,则@符号在参数中显示为常规字符.删除@符号有效.换句话说,在我的.bash_profile中,我有以下功能:
test_func() {
echo $1
}
在Vim中,我运行:
:! test_func @% #outputs @path/to/my/file
:! test_func % #outputs path/to/my/file
什么是@符号正在做什么以及为什么在将输出发送到bash函数时它的行为会有所不同?
我相信是:h expr-register:
register expr-register @r
@r contents of register ‘r’
The result is the contents of the named register,as a single string.
Newlines are inserted where required. To get the contents of the unnamed
register use @” or @@. See |registers| for an explanation of the available
registers.
When using the ‘=’ register you get the expression itself,not what it
evaluates to. Use |eval()| to evaluate it.
至于为什么你不需要:这可能是因为:h cmdline-special.
- Ex special characters cmdline-special
Note: These are special characters in the executed command line. If you want
to insert special things while typing you can use the CTRL-R command. For
example,“%” stands for the current file name,while CTRL-R % inserts the
current file name right away. See |c_CTRL-R|.
Note: If you want to avoid the special characters in a Vim script you may want
to use |fnameescape()|.
In Ex commands,at places where a file name can be used,the following
characters have a special meaning. These can also be used in the expression
function expand() |expand()|.
% Is replaced with the current file name. :_% c_%