如何设置vim来编辑Makefile和普通代码文件?


22

我正在使用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

我想做的是编辑诸如.js,.html之类的普通文件时,我希望选项卡使用4个空格而不是普通选项卡缩进。

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

我以为.vimrc中的上述设置将为我提供此功能,但不适用于我,因为当我编辑Makefile时,我仍然获得4个空格用于缩进。

不确定我在这里做错了吗?

Answers:


26

这是我的一部分.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

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

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

autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0

但是,请确保已打开文件类型检测。


感谢出色的自动命令!在从您那里学习的同时,我在本教程中注意到.vimrc,如果您不将autocmds 包装在augroup各个部分中,Vim将阅读这些内容并将其复制。它是否正确?
Joshua Detwiler

6

您可以使用每种文件类型创建自己的用户文件类型插件,而不是使用autocmds进行操作,然后将其放置在中~/.vim/ftplugin/<filetype>.vim,这<filetype>是您想要的实际文件类型。例如:

mkdir -p ~/.vim/ftplugin
echo "setlocal noexpandtab" > ~/.vim/ftplugin/make.vim

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

filetype plugin on

如果您希望保持.vimrc和.vim目录整洁,则此答案更有意义。
Floby

0

将vim配置为始终扩展制表符更简单,这是除makefiles之外的所有文件所需要的。在makefile文件中,您可以使用它在任意位置插入标签。它不会扩展。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.