如何在开括号后设置自动缩进 [英] How to set autoindent after open parenthesis
问题描述
当我输入一个左括号后跟一个换行符时,我希望光标自动缩进一个制表符值——就像它使用左花括号或方括号一样.出于某种原因,它缩进了两个标签值.
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.
我对让它对 .dart 文件正常工作特别感兴趣.
I'm particularly interested in getting this to work properly for .dart files.
这是我的 .vimrc:
Here's my .vimrc:
set tabstop=2
set softtabstop=2
set shiftwidth=2
set autoindent
set expandtab
我错过了什么?谢谢.
推荐答案
因此 vim 中有几个选项用于缩进(请参阅 :h C-indenting
以获得更好的理解)还有一篇关于 vimways 的很棒的文章 https://vimways.org/2019/indentation-without-dents/(强烈推荐)
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
这是最简单的一种,它没有做太多参见 :h 'smartindent'
自动插入缩进:
- 在以{"结尾的行之后.
- 在以 'cinwords' 中的关键字开头的一行之后.
- 在以 '}' 开头的行之前(仅适用于O"命令).
2.cindent
见 :h cindent
它可以覆盖 smartindent
它是你目前正在经历的事情(你想改变)
2. cindent
see :h cindent
it can overwrite smartindent
it kind of something you are currently experiencing ( which you want to change )
3.indentexpr
现在这是真正的交易,它很强大,大多数插件都使用这个选项,现在真正的问题是如何使用它
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
类似的东西
setlocal indentexpr=GetMyCustomIndent()
" Only define the function once
if exists("*GetMyCustomIndent") | finish | endif
function! GetMyCustomIndent()
return 0
endfunction
你可以创建你的函数vim会调用它来知道它需要插入多少个缩进
you can create your function vim will call it to know how many indentation it need to insert
结果应该是缩进的空格数(或-1保持当前缩进)为了尊重用户对 'shiftwidth'
的选择,返回 indentlvl * shiftwidth()
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'
returnindentlvl * shiftwidth()
正如你在这里看到的,这是一个非常强大的选项,你可以用它做很多事情,我建议阅读关于 vimways 的文章
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
这里的大部分内容都是我用 vim 文档和文章复制的,所以全部归功于 vim-doc 和 Axel Forsman 文章的作者
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
或者你可以使用插件
或
你可以复制缩进函数并做一些修改:p https://github.com/dart-lang/dart-vim-plugin/blob/master/indent/dart.vim
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
这篇关于如何在开括号后设置自动缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!