views:

17326

answers:

5

How do I make Vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like emacs does?

Also, how do I save these settings so I never have to input them again.

I've seen other questions related to this but it always seems to be a little off from what I want.

A: 

This .vimrc looks somewhat promising: http://www.dotfiles.com/files/9/53_.vimrc

warren
+44  A: 

in your .vimrc:

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

The help files take a bit of time to get used to but the more you read the better vim gets:

:help smartindent

Even better, you can embed these settings in your source for portability:

:help auto-setting

To see your current settings:

:set all

As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:

:help C-indenting
Ken
Thats the ticket, thanks ken!
Simucal
my pleasure - I've been using vim for 2 years now and I still learn something every day.
Ken
Thanks - smartindent (as a name) was new to me. I hadn't managed to work out which option did the trick on MacOS X.
Jonathan Leffler
Even with those settings applied, if I press ‘o’ in a line indented with spaces, the new line is indented with tabs :-( How can I change this behaviour?
Azat Razetdinov
If you have expandtab set then it should be using spaces. Do you also "set compatible"? That has various side effects including resetting expandtab to its default of "off"
Ken
Sorry, but smartindent was replaced by cindent, which itself is only appropriate for C-style syntax. Turning on either in your vimrc can be a problem when working with other languages. Just use "filetype indent on" instead.
graywh
Well, smartindent is *also* only for C-style syntax and is essentially deprecated.
graywh
A: 

The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.

As for tabs, there are two settings. Within vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.

You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.

Joey Gibson
+9  A: 

Related, if you open a file that uses both tabs and whitespace, assuming you've got

set expandtab ts=4 sw=4 ai

You can replace all the tabs with whitespace in the entire file with

:%retab
netjeff
+1  A: 

The best way to get filetype-specific indentation is to use ":filetype plugin indent on" in your vimrc. Then you can specify things like ":setl sw=4 sts=4 et" in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).

graywh
IMHO, better than the answer that has been marked correct. filetype indent on supersedes cindent and smartindent.