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.
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.
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'
.
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
For example, if tabstop=8
and softtabstop=3
, then, in Insert Mode:
<Tab>
on an empty line will insert 3 spaces, so that the total indentation is 3‐column wide;<Tab>
again will insert 3 more spaces, so that the total indentation is 6‐column wide;<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;<BS>
will undo step 3;<BS>
will undo step 2;<BS>
will undo step 1.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
If you want to use 2 spaces indentation, put this in your config:
set tabstop=2
set shiftwidth=2
set expandtab
set smartindent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With