這是一個基於內置perl縮進代碼的快速入侵(在indent/perl.vim
)。希望你可以用它來得到你想要做的。有關更多詳細信息,請參閱perl縮進代碼或indent目錄中另一個文件中的更詳細的註釋。
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
其中something
是你的文件類型(與路徑替換~/.vim
到vimfiles
如果你使用的是Windows。
您也可能想在文件的開頭要堅持這(但前提是沒有可能首先被加載一些其他縮進聲明):
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
這工作就像一個魅力感謝您這麼快的回答 – chibicode 2011-01-28 15:27:18