如何在 Vim 启动后手动加载 ftplugin?

发布于 2024-08-16 09:01:34 字数 425 浏览 9 评论 0原文

假设我将 .txt 文件加载到 Vim 中。然后我想更改 filetype=html 但我还想加载关联的 ftplugin。我该怎么做?

我尝试过类似的东西: :set filetype plugin on:set filtype plugin_name on 以及 :filetype plugin_name on 等等,但是我似乎无法手动加载 ftplugin。有什么建议吗?

我尝试过 :filetype=html:filetype plugin on 以及其他组合,但均无济于事。

编辑:我无法用任何答案“完全”解决这个问题(但这可能是我的配置的个别问题)。然而,皮埃尔的回答非常好,所以我给了他绿色的复选标记。

Say I load a .txt file into Vim. Then I want to change the filetype=html but I also want an associated ftplugin loaded. How can I do this?

I've tried stuff like: :set filetype plugin on and :set filtype plugin_name on and also :filetype plugin_name on etc etc., but I can't seem to manually load the ftplugin. Any suggestions?

I've tried :filetype=html and then :filetype plugin on and other combinations to no avail.

EDIT: I was not able to "completely" solve this with any of the answers (but it maybe something individual to my configuration). However, Pierre's answers were pretty darn good so I'm giving him the green check mark.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ㄖ落Θ余辉 2024-08-23 09:01:34

我相当确定,当您使用 :set ft=X 切换文件类型时,它会自动在 .vim/ftplugin 文件夹中加载关联的插件。例如 :set ft=html 将加载 .vim/ftplugin/html.vim,您可以在其中加载任何关联的插件。但是,与 html 文件关联的 BufEnterBufNew 插件加载将不会加载,因为设置新文件类型不会触发这些事件。因此,如果您的 .vimrc 中有使用 BufNewBufEnter 加载的 html 特定插件,您可能需要将它们放入 .vim/ftpluging/html.vim 文件中。

您始终可以将模型行添加到更改文件类型的文本文件中。例如

I'm fairly sure that when you switch a file's type using :set ft=X it will automatically load the associated plugin in your .vim/ftplugin folder. E.g. :set ft=html would load .vim/ftplugin/html.vim where you would load any associated plugins. However BufEnter and BufNew plugin loads associated with html files won't be loaded as setting a new filetype does not trigger these events. So if you have html specific plugins in your .vimrc that are loaded with BufNew or BufEnter you may want to put them in a .vim/ftpluging/html.vim file instead.

You could always add a modeline to your text file that changes the filetype. E.g. <!-- vim: ft=html -->.

绮烟 2024-08-23 09:01:34

刚刚遇到了同样的情况,即使我打开了 Perl 文件,ftplugins 也没有加载。结果我的 Vim 副本(来自 Git for Windows)没有附带 ftplugin.vimftplugof.vim

以下是我的情况意识到这一点:

  1. :filetype

    • 检查文件类型插件是否确实已打开。
  2. :scriptnames

    • 打印源脚本列表,发现没有来自ftplugin\的脚本。
    • 当时我没有意识到这一点,但我应该注意到我在那里没有看到 ftplugin.vim
  3. 检查 Vim 目录 (< code>{git-path}/share/vim/vim{version}/) 有 ftplugin.vimftplugof.vim

    • 这些文件是在执行 :filetype plugin on:filetype plugin off 时获取的

为了解决这个问题,我从 Vim 运行时文件 并复制到它应该去的地方。

Just had the same situation where ftplugins weren't loading even though I had a Perl file open. Turns out that my copy of Vim (from Git for Windows) didn't come with ftplugin.vim and ftplugof.vim

Here's how I arrived at that realization:

  1. :filetype

    • check that filetype plugin is indeed turned on.
  2. :scriptnames

    • print the list of sourced scripts, found out no script from ftplugin\ is sourced.
    • I didn't realize it at that time, but I should've noticed that I didn't see ftplugin.vim in there.
  3. check if Vim directory ({git-path}/share/vim/vim{version}/) has the ftplugin.vim and ftplugof.vim

    • these files are sourced when you execute :filetype plugin on and :filetype plugin off

To fix this, I grabbed the missing files from the Vim runtime files and copied to where it should go.

傾城如夢未必闌珊 2024-08-23 09:01:34
:filetype on
:set filetype=html
:filetype on
:set filetype=html
梦情居士 2024-08-23 09:01:34

当您更改文件类型时,Vim 会自动加载正确的插件

:set ft=python

您可以以详细模式启动 vim:

vim -V

这将显示 vim 正在执行什么。如果您打开文件并手动设置文件类型,vim 将显示它是否正在加载正确的插件。

Vim automatically loads the correct plugin when you change the filetype

:set ft=python

You can start up vim in verbose mode:

vim -V

This will show you what vim is executing. If you open a file and manually set the filetype, vim will show if it's loading the correct plugin.

别低头,皇冠会掉 2024-08-23 09:01:34
:f something.html
:w
:e
:f something.html
:w
:e
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文