307

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-space tabs)

How can I configure Vim to make it like this:

#selector {
    property: value;
}

(4-space tabs)

3ocene
  • 2,102
  • 1
  • 15
  • 30
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 5
    Do you want one tab of 4 spaces width? Four spaces? Or four tabs of 1 space width? They're different things, and you aren't clear. In any case, start with reading :he ts , :he sw , and :he expandt – abeyer Jan 13 '10 at 05:34

8 Answers8

406
: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
  • 102,279
  • 44
  • 260
  • 354
  • 155
    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 Jan 13 '10 at 05:54
  • 20
    Note that if you prefer **'>'** and **'<'** to move your text the same distance as the **tab**-key, you can simply `:set shiftwidth=0` in your vimrc and forget about it forever, because this tells vim that you always want it to match `tabstop`. – Seldom 'Where's Monica' Needy Apr 26 '17 at 01:22
  • 5
    [Developers Who Use Spaces Make More Money Than Those Who Use Tabs](https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/) – Cornel Masson Aug 13 '21 at 09:05
  • 4
    Tabs are better than spaces, you can't change my mind. – Danielr Sep 21 '21 at 21:10
  • 2
    spaces are the best imho – Vthechamp Sep 04 '22 at 14:56
217

To make the change for one session, use this command:

:set tabstop=4

To make the change permanent, add it to ~/.vimrc or ~/.vim/vimrc:

set tabstop=4

This will affect all files, not just css. To only affect css files:

autocmd Filetype css setlocal tabstop=4

as stated in Michał's answer.

Community
  • 1
  • 1
ki9
  • 5,183
  • 5
  • 37
  • 48
  • 2
    if you want tab to function as four spaces, it is necessary to add the other lines in the @Zoul answer to the .vimrc as well; that is, add 'set shiftwidth=4' and 'set expandtab' to the .vimrc too. – qartal Mar 04 '17 at 22:50
  • 12
    The question is "How do I change tab size in Vim?" – ki9 Apr 25 '17 at 02:36
153

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.

Community
  • 1
  • 1
Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
73

As a one-liner into vim:

:set tabstop=4 shiftwidth=4

For permanent setup, add these lines to ~/.vimrc:

set tabstop=4
set shiftwidth=4
set expandtab    <-- (optional) 4-spaces instead of Tab indentation
oz19
  • 1,616
  • 1
  • 17
  • 22
58

Several of the answers on this page are 'single use' fixes to the described problem. Meaning, the next time you open a document with vim, the previous tab settings will return.

If anyone is interested in permanently changing the tab settings:

Alexander McNulty
  • 870
  • 1
  • 11
  • 19
8

UPDATE

If you are working in a particular project I highly recommend using editorconfig.

It lets you define an .editorconfig file at the root of your repository defining the indentation you want to use for each file type across your repository.

For example:

root = true

[*.css]
charset = utf-8
indent_style = space
indent_size = 4

[*.js]
charset = utf-8
indent_style = space
indent_size = 2

There is a vim plugin that automatically configures vim according to the config file for file you open.

On top of that the .editorconfig file is automatically supported on many other IDEs and editors so it is the best option for collaborating between users with different environments.

ORIGINAL ANSWER

If you need to change sizes often and you don't want to bind this to a specific file type you can have predefined commands on your .vimrc file to quickly switch preferences:

nmap <leader>t :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>

This maps two different sets of sizes to keys \t and \m. You can rebind this to whatever keys you want.

Jens
  • 5,767
  • 5
  • 54
  • 69
3

in my .vim/vimrc (vim 8.0 under ubuntu bionic), I have

if has("autocmd")
  filetype plugin indent on
endif

so added line like : autocmd Filetype css setlocal tabstop=2 doesn't work.

I created .vim/indent folder and added in : css.vim with

set tabstop=2
set shiftwidth=2
set softtabstop=2

and it works !
I tried to on another computer with ubuntu focal and vim 8.1, it doesn't work !-(

bcag2
  • 1,988
  • 1
  • 17
  • 31
2

in vim command mode, write:

:set ts=X

where X is your new desired space length

Meysiolio
  • 71
  • 1
  • 1
  • 7