我使用的是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 个用于缩进的空格。
不确定我在这里做错了什么?
答案1
这是我的一部分.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
filetype
该部分应该是不言自明的,但我建议您阅读和的vim 帮助autocmd
。
对你来说最相关的可能是这一行:
autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0
但是,请确保文件类型检测已打开。
答案2
除了使用自动命令执行此操作外,您还可以为每种文件类型创建自己的用户文件类型插件,并将其放置在 中~/.vim/ftplugin/<filetype>.vim
,其中<filetype>
是您想要的实际文件类型。例如:
mkdir -p ~/.vim/ftplugin
echo "setlocal noexpandtab" > ~/.vim/ftplugin/make.vim
您确实需要确保已使用~/.vimrc
以下命令启用了文件类型插件:
filetype plugin on
答案3
将 vim 配置为始终展开制表符更为简单,这是除 makefile 之外的所有文件所希望的。在 makefile 中,您可以使用 在任何需要的地方插入制表符。它不会被展开。