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)

1

8 Answers

: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.

4

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.

2

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.

2

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 
3

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:

2

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.

0

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 !-(

in vim command mode, write:

:set ts=X 

where X is your new desired space length

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy