autocmd 检查vim 中的文件名?
我想写一些自动命令来检查文件名是这个还是那个..
我一直在尝试:
autocmd VimEnter * if % is "preamble" | Explore | endif
autocmd VimEnter * if %=="preamble" | Explore | endif
autocmd VimEnter * if &filename == "preamble" | Explore | endif
但是这些都不起作用?
我应该怎么办?
(这个例子过于简单,请注意,我还想有一个 else 语句)。
I want to write some autocmd's that check if the filename is this or that..
I've been trying:
autocmd VimEnter * if % is "preamble" | Explore | endif
autocmd VimEnter * if %=="preamble" | Explore | endif
autocmd VimEnter * if &filename == "preamble" | Explore | endif
but none of this works?
WHat should I do?
(this example is over-simplified, note that I'd also like to have an else statement).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该获取当前文件名
@%
。例如,echo @%
将为您提供文件名,而echo %
则不会。但是,对于文件上的操作,请使用%
,例如source %
。这可能应该适合您想做的事情:
You should get the current file name as
@%
. For e.g.,echo @%
will give you the filename, whereasecho %
will not. However, for actions on the file, use%
, e.g.source %
.This should probably work for what you want to do:
原帖中的示例有两个问题:
%
和&filename
都不会返回您使用的文件名。查看
expand()
函数的帮助,了解如何获取文件名::h Expand()
您忽略了 autocmd 中的槽,其中通常会指定 file-matching-pattern。 autocmd 中的第三个槽(在您的版本中为
*
)实际上是匹配文件名的模式。请参阅:h autocmd-patterns
。如果您想忽略模式槽,它应该可以正常工作,但如果是这样,您必须解决上面第 1 段中的问题。例如,autocmd BufRead,BufNewFile * if Expand('%') =~ "前导码" |探索 |结束
There are two problems with the examples in the original post:
Neither the
%
nor&filename
will return the filename as you've used them.Look at the help for the
expand()
function to see how to get the filename::h expand()
You're ignoring the slot in autocmd where a file-matching-pattern would ordinarily be specified. The third slot in the autocmd (
*
in your versions) is actually a pattern to match filenames. See:h autocmd-patterns
. It should work okay if you want to ignore the pattern slot, but if so, you've got to fix the problem in paragraph 1 above. E.g.,autocmd BufRead,BufNewFile * if expand('%') =~ "preamble" | Explore | endif
其他信息 在 autocmd 中使用 '@%'
某些 :
autocmd
事件 不支持@%
命名寄存器读取当前文件 姓名。在这些情况下,最好使用Additional Info Using '@%' with autocmd
Some
autocmd
events do not support the@%
named register to read the current file name. In these cases it is better to use <afile>: