简体   繁体   English

Vim文件内命令

[英]Vim In-File Commands

I'm after a means by which I can add additional commands to a text file via vim. 我正在寻找一种方法,通过它可以通过vim向文本文件中添加其他命令。 For example, just as you can do something like this: 例如,就像你可以这样做:

# vim:syntax=foo

I'd like to do something like: 我想做点什么:

# vim:option call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')

Any ideas? 有任何想法吗? I know I can write a syntax file, but this is not what I'm after for now. 我知道我可以写一个语法文件,但这不是我现在所追求的。

Vim modeline syntax (see :help modeline ) is not intended to specify commands to execute during file opening. Vim模式行语法(请参阅:help modeline )不用于指定在文件打开期间执行的命令。 That is exactly what autocommands is for (see :help autocommand ). 这正是autocommands的用途(参见:help autocommand )。 What you are trying to do should be an autocommand similar the following. 你要做的应该是一个类似以下的自动命令。

autocmd FileType foo call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')

or 要么

autocmd BufReadPost *.foo call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')

(Here instead of *.foo you can use any pattern that matches path or filename (or both) of the target file.) (这里代替*.foo您可以使用与目标文件的路径或文件名(或两者)匹配的任何模式。)

If the configuration you are setting up is local to some files or a project, and you don't want to pollute your .vimrc with those autocmd s, use localvimrc plugin. 如果您正在设置的配置是某些文件或项目的本地配置,并且您不想使用这些autocmd污染.vimrc ,请使用localvimrc插件。 It allows you to have a "local" .vimrc file next to your target file or project folder. 它允许您在目标文件或项目文件夹旁边有一个“本地” .vimrc文件。 Script stored in that .lvimrc is executed when you open files in the same directory where the "local" .vimrc is, or in its subdirectories. 当您在“local” .vimrc所在的同一目录中或在其子目录中打开文件时,将执行存储在该.lvimrc脚本。 Autocommands shown above (or any other configurations) can be stored in a .lvimrc file local the project. 上面显示的自动命令(或任何其他配置)可以存储在项目本地的.lvimrc文件中。 For details about localvimrc configuration see the homepage of the plugin. 有关localvimrc配置的详细信息,请参阅插件的主页。

This isn't an answer to your question, but I have also searched for Truth, and this question here is the closest one to it: 这不是你问题的答案,但我也搜索了真理,这里的问题是最接近它的问题:

Vim: How to execute selected text as vim commands Vim:如何将所选文本作为vim命令执行

It isn't automatic, but potentially only one keypress away it's close enough. 它不是自动的,但可能只有一个按键足够接近它。 :) :)

My ModelineCommands plugin extends Vim's built-in modelines to execute any Ex command(s) when a file is opened. 我的ModelineCommands插件扩展了Vim的内置模型,以便在打开文件时执行任何Ex命令。 A set of configurable validators examine the commands and can verify the correctness of an optional command digest, in order to prevent the execution of potentially malicious commands from unknown sources. 一组可配置的验证器检查命令并可以验证可选命令摘要的正确性,以防止从未知来源执行潜在的恶意命令。 (That's the main reason why Vim doesn't offer this feature!) This way, you could restrict the commands to only simple :let , or have the plugin query you to confirm execution of anything that isn't signed with your own secret key. (这就是为什么Vim不提供此功能的主要原因!)这样,您可以将命令限制为仅仅简单:let ,或:let插件查询您确认执行未使用您自己的密钥签名的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM