tags:

views:

702

answers:

2

Every time I add a selector in CSS and I press Enter to define the properties it ends up like this:

#selector {
        property: value;
}

(8 tabs)

How can I configure gVim to make it like this:

#selector {
    property: value;
}

(4 tabs)

+1  A: 
:set tabstop=4
:set shiftwidth=4
:set expandtab

This will insert four spaces instead of a tab character. Spaces are a bit more “stable”, meaning that text indented with spaces will show up the same in the browser and any other application.

zoul
Yes, but tabs are semantic (each tab means 1 layer of indentation), while spaces are purely presentational. Or in other words, using tabs will let other people viewing your code to display however wide they're comfortable with, whereas spaces wouldn't allow this.
K Prime
I don't think janoChen was trying to start a debate about tabs vs spaces, just asking how to modify vim's presentation of existing tabs.
Sam Post
+1  A: 

Expanding on zoul's answer:

If you want to setup Vim to use specific settings when editing a particular filetype, you'll want to use autocommands:

autocmd Filetype css setlocal tabstop=4

This will make it so that tabs are displayed as 4 spaces. Setting expandtab will cause Vim to actually insert spaces (the number of them being controlled by tabstop) when you press tab; you might want to use softtabstop to make backspace work properly (that is, reduce indentation when that's what would happen should tabs be used, rather than always delete one char at a time).

To make a fully educated decision as to how to set things up, you'll need to read Vim docs on tabstop, shiftwidth, softtabstop and expandtab. The most interesting bit is found under expandtab (:help 'expandtab):

There are four main ways to use tabs in Vim:

  1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.

  2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.

  3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.

  4. Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' is changed.

Michał Marczyk
Also, don't worry too much about possibly changing your mind later... see `:help retab` (check the ! variant).
Michał Marczyk
Thanks. shiftwidth was the answer. (I wanted to check zoul's answer too but I can't.
janoChen