Indenting in VIM

    I started the process of changing Komodo IDE to VIM by studying all kinds of tutorials and how-tos, however, surprisingly, in none of them I was able to meet a human description of the process of setting up indentation. Some suggested adjusting the width of the indentation using the tabstop option , others - using softtabstop , and thirdly - exposing both of them and shiftwidth in addition. After several hours of experimenting with the settings, I realized that the only way to not only 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 with a description of the options that can be used when setting indents.

    tabstop(default is 8) - the number of spaces by which the tab character is displayed in the text. It affects both existing tabs and new ones. If the value changes, "on the fly" is applied to the text.

    softtabstop (0) - the number of spaces by which the tab character is displayed when adding. Despite the fact that when you click on Tab you get the expected result (a new tab character is added), in fact indentation can use both tabs and spaces. For example, if tabstop is set to 8 and softtabstop is set to 4, pressing Tab three times will add an indentation of 12 spaces wide, but it will be formed from one tab character and 4 spaces.

    shiftwidth(8) - by default it is used to adjust the width of the indentation in spaces added by the >> and << commands. If the option value is not equal to tabstop , as is the case with softtabstop , the indentation can consist of both tabs and spaces. When you enable the option - smarttab , it has an additional effect.

    smarttab (off) - if this option is enabled, pressing Tab at the beginning of the line (to be more precise, before the first non-space character in the line) will add an indentation whose width corresponds to shiftwidth (regardless of the values ​​in tabstop and softtabstop) Clicking on Backspace will remove the indentation, and not just one character, which is very useful when expandtab is on . Let me remind you: the option only affects the indentation at the beginning of the line, in other places the values ​​from tabstop and softtabstop are used .

    expandtab (off) - in insert mode, replaces the tab character with the appropriate number of spaces. It also affects the indentation added by the >> and << commands.

    Armed with the acquired knowledge, it is very simple to adjust the required indentation behavior. A popular practice among developers: spaces instead of tabs, indent width - 4 spaces. The setup in this case will look like this:
    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 for development:

    autoindent (off) - copies indents from the current line when adding a new one.

    smartindent (off) - does the same as autoindent plus automatically indents in the “right” places. In particular, the indent is placed after a line that ends with a {, before a line that ends with a}, is deleted before the # if it follows the first in the line, etc. (more info help 'smartindent').

    That, in fact, is all.

    My configuration file regarding indentation is as follows: I would be grateful for any clarifications or additions.
    set tabstop=4
    set shiftwidth=4
    set smarttab
    set expandtab
    set smartindent


    Also popular now: