如何设置 vim 以编辑具有两种不同类型缩进的 Makefile 和普通代码文件?

for*_*own 24 vim tabs vimrc makefile

我使用的是 Mac OSX 10.7.5,.vimrc 的内容如下:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set shiftround  
set smarttab    
set autoindent  
set copyindent  

autocmd FileType make setlocal noexpandtab
Run Code Online (Sandbox Code Playgroud)

我想要做的是,当我编辑 .js、.html 等普通文件时,我希望我的制表符缩进 4 个空格而不是普通制表符。

但是当我编辑 Makefile 时,我需要它是一个普通的制表符,而不是 4 个用于缩进的空格。

我认为 .vimrc 中的上述设置会给我这个,但对我不起作用,因为当我编辑 Makefile 时,我仍然得到 4 个空格用于缩进。

不确定我在这里做错了什么?

pol*_*mon 31

这是我的一部分.vimrc

" enable filetype detection:
filetype on
filetype plugin on
filetype indent on " file type based indentation

" recognize anything in my .Postponed directory as a news article, and anything
" at all with a .txt extension as being human-language text [this clobbers the
" `help' filetype, but that doesn't seem to prevent help from working
" properly]:
augroup filetype
  autocmd BufNewFile,BufRead */.Postponed/* set filetype=mail
  autocmd BufNewFile,BufRead *.txt set filetype=human
augroup END

autocmd FileType mail set formatoptions+=t textwidth=72 " enable wrapping in mail
autocmd FileType human set formatoptions-=t textwidth=0 " disable wrapping in txt

" for C-like  programming where comments have explicit end
" characters, if starting a new line in the middle of a comment automatically
" insert the comment leader characters:
autocmd FileType c,cpp,java set formatoptions+=ro
autocmd FileType c set omnifunc=ccomplete#Complete

" fixed indentation should be OK for XML and CSS. People have fast internet
" anyway. Indentation set to 2.
autocmd FileType html,xhtml,css,xml,xslt set shiftwidth=2 softtabstop=2

" two space indentation for some files
autocmd FileType vim,lua,nginx set shiftwidth=2 softtabstop=2

" for CSS, also have things in braces indented:
autocmd FileType css set omnifunc=csscomplete#CompleteCSS

" add completion for xHTML
autocmd FileType xhtml,html set omnifunc=htmlcomplete#CompleteTags

" add completion for XML
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags

" in makefiles, don't expand tabs to spaces, since actual tab characters are
" needed, and have indentation at 8 chars to be sure that all indents are tabs
" (despite the mappings later):
autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0

" ensure normal tabs in assembly files
" and set to NASM syntax highlighting
autocmd FileType asm set noexpandtab shiftwidth=8 softtabstop=0 syntax=nasm
Run Code Online (Sandbox Code Playgroud)

该部分应该是不言自明,但我建议你阅读vim的帮助filetypeautocmd

与您最相关的一行可能是这一行:

autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0
Run Code Online (Sandbox Code Playgroud)

不过,请确保文件类型检测已打开。


Hep*_*ite 9

您可以为每个文件类型创建自己的用户文件类型插件,而不是使用 autocmds 执行此操作,并将其放置在 中~/.vim/ftplugin/<filetype>.vim<filetype>您想要的实际文件类型在哪里。例如:

mkdir -p ~/.vim/ftplugin
echo "setlocal noexpandtab" > ~/.vim/ftplugin/make.vim
Run Code Online (Sandbox Code Playgroud)

您确实需要~/.vimrc使用以下命令确保在您的文件中启用了文件类型插件:

filetype plugin on
Run Code Online (Sandbox Code Playgroud)