16

I just discovered I could indent and un-indent easily in Vi with : << and >>

By default it adds a tab character. Now I'm wondering, could this be to a number of spaces via a configuration in my .vimrc? And how should I do it?

20

Several settings controls the behavior:

First set expandtab allows to replace the tabs by white spaces characters :h 'expandtab'.

Then set shiftwidth=4 makes the tabulations be 4 white spaces :h 'shiftwidth'.

You could also be interested by by :h 'tabstop' which defines the number of spaces that a tab character in the file counts for.

As a bonus see :h 'smartindent' and :h 'autoindent' which are pretty useful.

6

I answered my need by modifying .vimrc like below:

:set shiftwidth=2
:set autoindent
:set smartindent
~

Thanks Sibi & satox

  • also :set tabstop=2 – Straff Nov 2 '18 at 20:11
3

Yes.

You can set shiftwidth to any number of spaces. This works both in vi and vim.

:set shiftwidth=2

Now, the width shifted for each >> and << will be 2.

1
touch ~/.vimrc
echo "set ts=4 sw=4" >> ~/.vimrc

Shorthand for J.Chomel's answer.
touch will create the file .vimrc in your home directory if it doesn't exists yet
Next, append tabstop and shiftwidth with your preferences to the .vimrc file.

New contributor
aerijman is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Note that it is considered a best practice to use options long name in your vimrc (i.e. tabstop and shiftwidth) it is usually more readable and since your don't need to type them that often in your config file there is not much gain to use short names. – statox 2 days ago

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.