so there are couple of options in vim for indentation ( please see :h C-indenting
for better understanding )
also there is a great article on vimways https://vimways.org/2019/indentation-without-dents/ ( highly recommended)
1. smartindent
This is the simplest one it does not do much see :h 'smartindent'
An indent is automatically inserted:
- After a line ending in '{'.
- After a line starting with a keyword from 'cinwords'.
- Before a line starting with '}' (only with the "O" command).
2. cindent
see :h cindent
it can overwrite smartindent
it kind of something you are currently experiencing ( which you want to change )
3. indentexpr
now this is the real deal it's powerful and most of the pluggin out there use this option now the real question is how to use it
something like this
setlocal indentexpr=GetMyCustomIndent()
" Only define the function once
if exists("*GetMyCustomIndent") | finish | endif
function! GetMyCustomIndent()
return 0
endfunction
you can create your function vim will call it to know how many indentation it need to insert
The result should be the number of spaces of indentation (or -1 for keeping the current indent)
To honor the user’s choice of 'shiftwidth'
return indentlvl * shiftwidth()
so as you can see here this is very powerful option you can do a whole lot with this I recommend reading the article on vimways
most of the stuff here i copied with vim documentation and from the article so full credit goes to vim-doc and Axel Forsman the author of the article
or you can use a plugin
or
you can copy there indent function and do some modification :p https://github.com/dart-lang/dart-vim-plugin/blob/master/indent/dart.vim
function! DartIndent()
" Default to cindent in most cases
let indentTo = cindent(v:lnum)
let previousLine = getline(prevnonblank(v:lnum - 1))
let currentLine = getline(v:lnum)
" Don't indent after an annotation
if previousLine =~# '^\s*@.*$'
let indentTo = indent(v:lnum - 1)
endif
" Indent after opening List literal
if previousLine =~# '\[$' && !(currentLine =~# '^\s*\]')
let indentTo = indent(v:lnum - 1) + &shiftwidth
endif
return indentTo
endfunction