加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 获取当前文件名称的Vim语法

发布时间:2020-12-15 18:19:48 所属栏目:安全 来源:网络整理
导读:在Vim中,我可以使用以下命令回显当前文件名: :echo @% 我在这里找到了这些信息:http://vim.wikia.com/wiki/Get_the_name_of_the_current_file 有人可以解释为什么@符号是必要的吗?如果我输入没有@符号的命令,我会收到一个错误: E15: Invalid expression:
在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.

  1. 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_%

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读