8

When I type an open parenthesis followed by a newline, I'd like the cursor to autoindent one tab value--the same way it does with an open curly brace or open square bracket. For some reason, it indents two tab values.

I'm particularly interested in getting this to work properly for .dart files.

Here's my .vimrc:

set tabstop=2
set softtabstop=2
set shiftwidth=2
set autoindent
set expandtab

What am I missing? Thank you.

rampatowl
  • 1,260
  • 1
  • 9
  • 26
  • 1
    Depends on the filetype, probably. Btw, recommended not to mess with tabstop (it controls how wide a real tab character appears, which you cant always control outside of vim. Better to leave it alone, so you always see tabs the same width, and use spaces for alignment if you must) – D. Ben Knoble Jan 12 '20 at 04:31
  • Thanks for the comment. Removed tabstop but still getting the same issue. Particularly interested in getting this to work for .dart files. – rampatowl Jan 12 '20 at 21:03
  • You could define this map: `inoremap ( ()` – MAGA Jan 19 '20 at 05:02

1 Answers1

4

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
Tripurari Shankar
  • 2,806
  • 10
  • 21