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?
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?
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.
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
v to get into visual mode== is an operator.
– D. Ben Knoble
Feb 10 '21 at 13:46
= 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.
– geedoubleya
Feb 25 '21 at 17:59
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.
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 '19 at 07:18
tabstopis effectively how many columns of whitespace a\tcharacter is worth. 2)shiftwidthis how many columns of whitespace a “level of indentation” is worth. The>action indents by 1 level. 3)softtabstopis how many columns of whitespace atab keypressor abackspace keypressis worth. ... More on tab settings in vim in a medium article I wrote https://medium.com/@arisweedler/tab-settings-in-vim-1ea0863c5990