在vim中使用Matlab进行自动命令?

Ben*_*kes 5 vim

我每天都使用几种不同的编程语言,我希望每种语言都有不同的标签宽度(空格).例如:我使用Ruby的"标准"2空格,但我们现有的所有Matlab代码都使用4个空格.

我个人提到这个~/.vimrc:

augroup lang_perl
    au!
    set tabstop=4 " tabstop length N in spaces
    set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
    set expandtab " Use spaces instead of tabs
augroup END

augroup lang_ruby
    au!
    set tabstop=2 " tabstop length N in spaces
    set shiftwidth=2 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
    set expandtab " Use spaces instead of tabs
augroup END
Run Code Online (Sandbox Code Playgroud)

这些工作,但以下不是:

augroup lang_matlab
    au!
    set tabstop=4 " tabstop length N in spaces
    set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
    set expandtab " Use spaces instead of tabs
augroup END
Run Code Online (Sandbox Code Playgroud)

我真的不明白augroup lang_ruby我是在编辑一个Ruby文件.(我的搜索提出来了ftdetect,但解决方案并不明显.)似乎并不vim知道我正在编辑Matlab使用augroup lang_matlab.我做了什么改变才能使这项工作?

ZyX*_*ZyX 2

如果您想要对 {filetype} 有大量设置,您应该将它们放入~/.vim/ftplugin/{filetype}.vim或放入匹配的文件中~/.vim/ftplugin/{filetype}/**/*.vim(例如:~/.vim/ftplugin/ruby/foo.vim, ~/.vim/ftplugin/ruby/foo/bar.vim)。在这种情况下,您根本不需要任何自动命令。如果您仍想使用自动命令,请使用以下命令:

augroup lang_matlab
    autocmd!
    autocmd FileType matlab      setlocal ts=4 sw=4 et
augroup END
Run Code Online (Sandbox Code Playgroud)

。请注意两件事: FileType 事件(它在那里,但不是 BufRead、BufNewFile),而setlocal不是普通的set. 第一个用于文件类型设置,第二个是如何设置特定于缓冲区的选项。

关于为什么 perl 和 ruby​​ 设置有效以及为什么 matlab 设置无效:您的示例代码与

augroup lang_perl
    autocmd!
augroup END
augroup lang_ruby
    autocmd!
augroup END

set tabstop=4 " tabstop length N in spaces
set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs

set tabstop=2 " tabstop length N in spaces
set shiftwidth=2 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
Run Code Online (Sandbox Code Playgroud)

所以,你有效地设置了ts=2 sw=2 et. 但$VIMRUNTIME/ftplugin/perl.vim包含以下代码:

setlocal  tabstop=4
setlocal  shiftwidth=4
Run Code Online (Sandbox Code Playgroud)

所以,ts=4 sw=4for perl 设置为ftplugin/perl.vim而不是在你的 vimrc 中(如果你已经安装了 perl-support 插件)。您可以通过在 vimrc 中替换为来检查tabstop=4tabstop=8