62

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?

1
  • 2
    1) tabstop is effectively how many columns of whitespace a \t character is worth. 2) shiftwidth is how many columns of whitespace a “level of indentation” is worth. The > action indents by 1 level. 3) softtabstop is how many columns of whitespace a tab keypress or a backspace keypress is worth. ... More on tab settings in vim in a medium article I wrote medium.com/@arisweedler/tab-settings-in-vim-1ea0863c5990 Apr 10, 2020 at 23:05

5 Answers 5

68

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.

0
25

In fact, aside from other answers, it is also possible to use autoindent (to automatically indent newlines) and smartindent (which works with C-like braces languages less strictly than cindent). Then the shiftwidth I wanted was 2.

I modified my $HOME/.vimrc like this:

:set shiftwidth=2
:set autoindent
:set smartindent
6
  • 8
    also :set tabstop=2
    – Straff
    Nov 2, 2018 at 20:11
  • 1
    I think it would be really helpful to add more explanations as to what the different lines do Apr 9, 2020 at 15:23
  • @J.Chomel meta.stackoverflow.com/questions/300837/… Apr 12, 2020 at 12:05
  • 1
    @J.Chomel actually none of the other answers here explain what autoindent or smartindent do. I think that even if that was the case, either your answer is a comment, in which case it can exempt itself from explanation, or it's a full blown answer, in which case it should not be code only if it's not self-explanatory Apr 12, 2020 at 12:06
  • 1
    @J.Chomel I am just trying to make it easier for everyone to learn. Good job on editing your answer, I think it is way more helpful now Apr 15, 2020 at 9:01
6

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.

2
  1. press esc then v to get into visual mode
  2. select the lines which you want to modify the indents
  3. and then press =
2
  • 4
    Welcome to Vi and Vim! This doesn't affect tabs vs spaces; it simply re-indents the lines. And visual mode is not strictly necessary, given that = is an operator.
    – D. Ben Knoble
    Feb 10, 2021 at 13:46
  • 1
    +1 doing that changed the indent size of my existing code and that is what the title asks, so thanks. @D.BenKnoble the = worked as it should in visual mode, and in non visual mode is ignored in place of the + which adds a line above. Not sure what you mean. Feb 25, 2021 at 17:59
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.

1
  • 8
    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
    Aug 22, 2019 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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