使用单个 `.vimrc`(不带 ftplugin)按文件类型更改 Vim 行为 [英] Changing Vim behavior by file type using single `.vimrc` (without ftplugin)

查看:29
本文介绍了使用单个 `.vimrc`(不带 ftplugin)按文件类型更改 Vim 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用单个 .vimrc 文件(没有 ftplugin)以优雅的方式更改 Vim 行为?

How to change Vim behaviour using single .vimrc file (without ftplugin) in elegant way ?

我的意思是...如果我为 C/C++ 文件运行很多命令,例如:

What do I mean is ... if I run many commends for C/C++ file , e.g.:

set nu
set cin
set ai
set mouse=a
color elflord

还有另外一堆对 AsciiDoc 文件的推荐,例如:

and another bunch of commends for AsciiDoc files, e.g.:

set syntax=asciidoc
set nocin
set spell spl=en

没有提到 Python、LaTeX 等......

Not mentioning about Python, LaTeX etc....

有解决方案https://stackoverflow.com/a/159065/544721用于将 autocommand 放在每个自定义命令之前.

There is solution presented https://stackoverflow.com/a/159065/544721 for putting autocommand before every custom command.

是否有一种很好的方法可以在不使用 ftplugin 的情况下为 autocommand 将它们分组 - 以便将所有内容保存在单个 .vimrc 文件中(更好用于移动许多机器等)?与带括号 {} ?

Is there a nice way to group them for autocommand without using ftplugin - in order to keep everything in single .vimrc file (better for moving around many machines, etc.) ? Something equivalent to if statement with brackets {} ?

推荐答案

您可以使用以下内容:

if has("autocmd")
    augroup LISP
        au!
        au BufReadPost *.cl :set lisp
        au BufReadPost *.cl :set showmatch
        au BufReadPost *.cl :set cpoptions-=m
        au BufReadPost *.cl :set autoindent
    augroup END
    augroup C
        au!
        autocmd BufNewFile,BufRead *.cpp set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4
        autocmd BufNewFile,BufRead *.c set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4
        autocmd BufNewFile,BufRead *.h set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4
        autocmd BufNewFile,BufRead *.cpp set tw=80
        autocmd BufNewFile,BufRead *.c set tw=80
        autocmd BufNewFile,BufRead *.h set tw=80
    augroup END
endif

这根据打开的文件类型创建了命令分组,这在 autocmd 部分中指定.您仍然需要在每个之前指定 autocmd 或 au,但它们很好地分组.

This created grouping of commands depending on the type of file that is opened, which is specified in the autocmd section. You still need to specify autocmd or au before each one, but they are nicely grouped.

这篇关于使用单个 `.vimrc`(不带 ftplugin)按文件类型更改 Vim 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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