在 .vim 和 .vimrc 中组织文件类型设置的最佳方式? [英] Best way to organize filetype settings in .vim and .vimrc?

查看:31
本文介绍了在 .vim 和 .vimrc 中组织文件类型设置的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览我的 vim dotfiles 来整理它们.我注意到,随着时间的推移,我以各种不一致的方式添加了各种特定于文件类型的设置.假设我正在为 Python 进行自定义:

I'm going through my vim dotfiles to tidy them up. I've noticed that through time I've added various filetype specific settings in various inconsistent ways. Let's suppose I'm customizing for Python:

  • au BufRead,BufNewFile *.py(做某事).我不喜欢这样,因为某些 Python 文件可能没有 .py 终止.

  • au BufRead,BufNewFile *.py (do something). I don't like this because some Python files might not have the .py termination.

au FileType python(做点什么).这似乎是一个更好的选择,因为它不依赖于具有 .py 终止的文件.缺点是 Vim 不知道某些文件类型.我可以让 Vim 识别其他文件类型,但我也有各种不一致的方法:一个 .vim/filetype.vim 文件,另一个在 .vim/after/filetype.vimcode> 和 .vimrc 中的各种 set filetype 命令.

au FileType python (do something). This seems a better option because it doesn't depend on the file having the .py termination. The drawback is that Vim doesn't know about some filetypes. I can make Vim recognize additional filetypes, but I also have various inconsistent ways of doing it: a .vim/filetype.vim file, another in .vim/after/filetype.vim and various set filetype commands in .vimrc.

添加带有文件类型特定设置的 .vim/ftplugin/python.vim 文件.我知道 $VIMRUNTIME/ftplugin/python.vim 可以覆盖我在这里所做的任何设置.一个问题是我不确定它是如何与 .vim/filetype.vim.vim/after/filetype.vim 交互的.

Add a .vim/ftplugin/python.vim file with filetype specific settings. I understand the $VIMRUNTIME/ftplugin/python.vim can override whatever settings I make here. One problem is that I'm not sure how this interacts with .vim/filetype.vim and .vim/after/filetype.vim.

添加一个 .vim/after/ftplugin/python.vim.我知道这是在 $VIMRUNTIME/ftplugin/python.vim 之后加载的,因此它可以从那里覆盖设置.和之前的方法一样,我不确定它是如何与 filetype.vim 文件交互的.

Add a .vim/after/ftplugin/python.vim. I understand that this is loaded after $VIMRUNTIME/ftplugin/python.vim so it can overwrite settings from there. As in the previous method I'm not sure how it interacts with the filetype.vim files.

所以我至少有四种方法可以做到这一点,没有提到语法文件和特定于文件类型的插件.在我看来,最好的方法是将我的文件类型特定设置放在 after/ftplugin 中,这样它们就不会被覆盖,而 filetypes.vim 中>after 出于同样的原因.

So I have at least four ways of doing this, not mentioning syntax files and filetype-specific plugins. It seems to me the best way to do this is to put my filetype specific settings in after/ftplugin so they don't get overwritten, and filetypes.vim in after for the same reason.

但是,在我继续之前,我想问一下是否有人对处理文件类型特定设置的最佳方法有任何建议.

However, before I proceed I'd like to ask if anyone has suggestions about the best way to deal with filetype specific settings.

推荐答案

我将我的 Python 特定设置存储在 $HOME/.vim/ftplugin/python.vim,因为我没有做任何与 $VIMRUNTIME/ftplugin/python.vim.

I store my Python-specific settings is in $HOME/.vim/ftplugin/python.vim, since I do nothing to conflict with $VIMRUNTIME/ftplugin/python.vim.

将这些设置保存在 ftplugin 文件中保持 我的 .vimrc 美观、通用且更容易维护(在使用 Vim 十多年后它仍然相当大).

Keeping these settings in an ftplugin file keeps my .vimrc nice and generic and easier to maintain (it's still pretty big after over ten years of Vim usage).

如果你想推翻什么ftplugins 与您的 Vim 发行版一起设置,然后 $HOME/.vim/after/ftplugin/python.vim 就是您想要的,因为它被读取 after这些.

If you want to overrule what the ftplugins with your Vim distribution set up, then $HOME/.vim/after/ftplugin/python.vim is what you want, as it is read after these.

类似地,$HOME/.vim/filetype.vim 将在 $VIMRUNTIME/filetype.vim 之前获得,而后者将在 $HOME 之前获得/.vim/after/filetype.vim.

Similarly $HOME/.vim/filetype.vim will be sourced before $VIMRUNTIME/filetype.vim and that in turn will be sourced before $HOME/.vim/after/filetype.vim.

调用 :scriptnames 将列出所有来源脚本名称,按照它们最初来源的顺序.

Invoking :scriptnames will list all sourced script names, in the order they were first sourced.

:help filetype 提供了相当全面的信息关于这一切.

:help filetype provides pretty comprehensive information on all of this.

这篇关于在 .vim 和 .vimrc 中组织文件类型设置的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆