这是一个快速破解,基于内置的 perl 缩进代码(in indent/perl.vim
)。希望您可以使用它来获得您想要做的事情。有关更多详细信息,请参阅 perl 缩进代码或缩进目录中的另一个文件中的更详细注释。
setlocal indentexpr=GetMyIndent()
function! GetMyIndent()
let cline = getline(v:lnum)
" Find a non-blank line above the current line.
let lnum = prevnonblank(v:lnum - 1)
" Hit the start of the file, use zero indent.
if lnum == 0
return 0
endif
let line = getline(lnum)
let ind = indent(lnum)
" Indent blocks enclosed by {}, (), or []
" Find a real opening brace
let bracepos = match(line, '[(){}\[\]]', matchend(line, '^\s*[)}\]]'))
while bracepos != -1
let brace = strpart(line, bracepos, 1)
if brace == '(' || brace == '{' || brace == '['
let ind = ind + &sw
else
let ind = ind - &sw
endif
let bracepos = match(line, '[(){}\[\]]', bracepos + 1)
endwhile
let bracepos = matchend(cline, '^\s*[)}\]]')
if bracepos != -1
let ind = ind - &sw
endif
return ind
endfunction
将该文件保存为您的文件类型~/.vim/indent/something.vim
所在的位置(如果您在 Windows 上,请替换为路径。something
~/.vim
vimfiles
您可能还希望将其粘贴在文件的开头(但前提是没有可能首先加载的其他缩进声明):
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1