I started the process of changing the Komodo IDE to VIM by studying all sorts of tutorials and hows that, however, surprisingly, in none of them I could find a human description of the indenting process. In some, it was suggested to adjust the indent width using the
tabstop option, in others - with the help of
softtabstop , in the third - to set both the one and the other and the
shiftwidth in addition. After several hours of experimenting with the settings, I realized that the only way not only to make everything work, but also to understand why it works, is to read the documentation. I want to share my “discoveries” with you.
I'll start by describing options that can be used when setting up indents.
tabstop (default 8) - the number of spaces by which the tab character is displayed in the text. It influences both existing tabs and new ones. In case of a change in value, "on the fly" is applied to the text.
')
softtabstop (0) - the number of spaces by which the tab character is displayed when added. Despite the fact that when you click on Tab, you get the expected result (a new tab character is added), in fact, both tabs and spaces can be used in the indent. For example, if
tabstop is set to 8 and
softtabstop is 4, pressing Tab 3 times will add an indentation of 12 spaces wide, but it will be formed from one tab character and 4 spaces.
shiftwidth (8)
—the default is used to adjust the width of indentations in the spaces added by the >> and << commands. If the value of the option is not equal to the
tabstop , as is the case with
softtabstop , the indentation may consist of both tab characters and spaces. When enabled, the
smarttab option has an additional effect.
smarttab (disabled) - if this option is enabled, pressing Tab at the beginning of the line (to be more precise, before the first non-white character in the line) will add an indent whose width corresponds to the
shiftwidth (regardless of the values ​​in the
tabstop and
softtabstop ). Clicking on Backspace will remove the indent, not just one character, which is very useful when the
expandtab is enabled. Let me remind you: the option only affects the indents at the beginning of the line, in other places the values ​​from the
tabstop and
softtabstop are used .
expandtab (off) - in insert mode, replaces the tab character with the appropriate number of spaces. It also affects the padding added by the >> and << commands.
Armed with the knowledge gained, it is very easy to set up the required indentation behavior. Popular practices among developers: spaces instead of tabs, indentation width - 4 spaces. The setting in this case will be as follows:
set tabstop=4
set shiftwidth=4
set smarttab
set expandtab
For dessert, I left a description of two life changing options for those who use VIM as an editor to design:
autoindent (off) - copies indents from the current line when adding a new one.
smartindent (off) - does the same as
autoindent plus automatically indents the “right” places. In particular, the indent is placed after the line that ends with the {character, before the line that ends with the} character, is deleted before the # character, if it follows first in the line, etc. (more help 'smartindent').
That's all.
My configuration file in terms of indents is as follows:
set tabstop=4
set shiftwidth=4
set smarttab
set expandtab
set smartindent
I would be grateful for any clarifications or additions.