为多种语言创建多个 .vimrc?

Creating multiple .vimrc for multiple languages?

我开始学习编程,看到可以通过.vimrc 文件配置vim。但是我在创建多个设置时遇到了问题。我想要 java 的配置和 C 的另一个配置,但我不知道如何只在使用特定语言编程时启用配置。

我找到了以下解决问题的方法:

If it's okay to configure the local exceptions centrally, you can put such autocmds into your ~/.vimrc:

:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4

(How to load different .vimrc file for different working directory?)

但是我不明白那个解决方案。

我想我应该做以下事情:

  1. 我想在桌面上创建一个C语言设置。所以我投入 Shell(我正在使用 Mac OS)以下命令 vim ~/Desktop/.vimrc 并将所需的配置放入此文件中。

  2. 然后我在文件~/.vimrc中放入以下命令:autocmd BufRead,BufNewFile ~/Desktop/.vimrc setlocal ts=4 sw=4

  3. 然后我进入 Shell 并创建了一个 C 文件 vim ~/Desktop/myprogram.c,但我意识到我的设置不起作用。

很明显我做错了,但是我找不到错误,因为我还是菜鸟。

为此你需要 3 个文件:~/.vimrc 和两个脚本 C-settings.vimjava-settings.vim

在第一个文件 ~/.vimrc 中,您需要包含这些自动命令:

"To enable file type detection"
filetype on
augroup Java_C_Settings
    "the command below execute the script for the specific filetype C
    autocmd FileType c source /path-for-C-settings/C-settings.vim

    "the command below execute the script for the specific filetype Java
    autocmd FileType java source /path-for-Java-settings/Java-settings.vim
augroup END 

在其他文件 (C-settings.vimJava-settings.vim) 中,您为每种类型的文件设置所需的设置 *.c*.java

示例:

C-settings.vim

set number
colorscheme blue

Java-settings.vim

set number
colorscheme evening

每当您使用 vim 打开文件时,后者将首先检查文件类型并自动配置设置。


注意:如果设置文件不在同一目录下可以重命名.exrc.

使用专用于此目的的 ftplugins。

将您的 C 配置放在 ~/.vim/ftplugin/c 中,依此类推。别忘了激活他们的支持。

(这实际上是一个重复的问题)