vimneovim

Setting autoindentation to spaces in neovim?


I was wondering how I could set the autoindentation to four spaces upon startup in neovim, because I use spaces for indentation.

Thanks in advance.


Solution

  • I don’t know Neovim specifically, but (from what I read there) I guess that it is compatible with Vim on this topic. So explanations below apply to pure Vim.

    The option you are looking for is 'expandtab'. However, for clarity, I explain indenting width before going into this option.

    Indenting width(s)

    The width of an indentation is controlled by several options. By “indenting”, here, I mean for example pressing <Tab> in Insert Mode (or <BS>, backspace, which undoes an existing indentation), or increasing the indenting level automatically (depending on the language).

    :help tabstop
    :help softtabstop
    :help shiftwidth
    

    The integer option 'tabstop' dictates the width used to display an actual tabulation character (\t) (not directly what you are interested in, but see below).

    The integer option 'softtabstop' says how wide an indentation is supposed to span. The special value 0 means to replicate the value of 'tabstop' (or more exactly, to disable the “soft tab stops” feature), the special value −1 means to replicate the value of 'shiftwidth'.

    The integer option 'shiftwidth' gives the width used for shifting commands, such as <<, >> and ==. The special value 0 means to replicate the value of 'tabstop'.

    Indenting with spaces

    When 'expandtab' is set, indenting is always done using space characters only. Otherwise, pressing <Tab> inserts as many tabulation characters as possible, and complete with space characters up to the indenting width.

    :help expandtab
    

    An illustration

    For example, if tabstop=8 and softtabstop=3, then, in Insert Mode:

    1. pressing <Tab> on an empty line will insert 3 spaces, so that the total indentation is 3‐column wide;
    2. pressing <Tab> again will insert 3 more spaces, so that the total indentation is 6‐column wide;
    3. pressing <Tab> will make the total indentation 9‐column wide; if 'expandtab' is set, then it will be written using a total of 9 spaces; otherwise, it will be written using a tabulation character (which replaces former whitespaces) followed by a space character;
    4. pressing <BS> will undo step 3;
    5. pressing <BS> will undo step 2;
    6. pressing <BS> will undo step 1.

    Example configuration

    Most often, you want to make it simple and have the same value for the three width options. Here is a example configuration that identifies all three options, so that you just need to change the value of 'tabstop' to your liking. It also sets 'expandtab' as you requested. Finally, since you evoked auto‐indentation, I included related options: 'autoindent', 'smartindent' and 'cindent'; but you should rather use language‐specific plugins for that.

    " length of an actual \t character:
    set tabstop=4
    " length to use when editing text (eg. TAB and BS keys)
    " (0 for ‘tabstop’, -1 for ‘shiftwidth’):
    set softtabstop=-1
    " length to use when shifting text (eg. <<, >> and == commands)
    " (0 for ‘tabstop’):
    set shiftwidth=0
    " round indentation to multiples of 'shiftwidth' when shifting text
    " (so that it behaves like Ctrl-D / Ctrl-T):
    set shiftround
    
    " if set, only insert spaces; otherwise insert \t and complete with spaces:
    set expandtab
    
    " reproduce the indentation of the previous line:
    set autoindent
    " keep indentation produced by 'autoindent' if leaving the line blank:
    "set cpoptions+=I
    " try to be smart (increase the indenting level after ‘{’,
    " decrease it after ‘}’, and so on):
    "set smartindent
    " a stricter alternative which works better for the C language:
    "set cindent
    " use language‐specific plugins for indenting (better):
    filetype plugin indent on
    

    You can tweak these settings and write them down in your .vimrc or .nvimrc file.

    Of course, additionally, you can select specific settings for each buffer, based on its filetype. For example:

    " do NOT expand tabulations in Makefiles:
    autocmd FileType make setlocal noexpandtab
    
    " for the C language, indent using 4‐column wide tabulation characters,
    " but make <Tab> insert half‐indentations as 2 spaces (useful for labels):
    autocmd FileType c setlocal noexpandtab shiftwidth=2
    
    " use shorter indentation for Bash scripts:
    autocmd FileType sh setlocal tabstop=2