Como configurar o vim para editar arquivos de código normais e Makefile?


22

Estou usando o Mac OSX 10.7.5, o conteúdo do .vimrc é o seguinte:

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

autocmd FileType make setlocal noexpandtab

O que estou tentando fazer é quando edito arquivos normais como .js, .html e quero que minhas guias sejam recuadas com 4 espaços em branco em vez de uma guia normal.

Mas quando estou editando o Makefile, preciso que ele seja uma guia normal em vez de 4 espaços em branco para recuos.

Eu pensei que a configuração acima em .vimrc vai me dar isso, mas não está funcionando para mim, como quando estou editando o Makefile, ainda estou recebendo 4 espaços em branco para o recuo.

Não tem certeza do que estou fazendo de errado aqui?

Respostas:


26

Esta é uma seção da minha .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

A seção deve ser auto-explicativa, mas sugiro que você leia a ajuda do vim em filetypee autocmd.

A linha mais relevante para você é provavelmente esta:

autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0

verifique se a detecção do tipo de arquivo está ativada.


Obrigado pelos excelentes comandos automáticos! Notei neste tutorial enquanto aprendia com o seu .vimrcque, se você não envolver seus autocmds em augroupseções, o Vim os lerá e os duplicará. Isso está correto?
Joshua Detwiler

6

Em vez de fazer isso com autocmds, você pode criar seu próprio plug-in de tipo de arquivo de usuário para cada tipo de arquivo e colocá-lo ~/.vim/ftplugin/<filetype>.vimonde <filetype>é o tipo de arquivo real que você deseja. Por exemplo:

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

Você precisa ter os plugins de tipo de arquivo ativados no seu ~/.vimrccom o seguinte comando:

filetype plugin on

Essa resposta faz mais sentido se você deseja manter os diretórios .vimrc e .vim em ordem.
Floby

0

É mais simples configurar o vim para sempre expandir as guias, o que é desejado para todos os arquivos, exceto makefiles. Nos makefiles, você pode usar para inserir uma guia onde quiser. Não será expandido.

Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.