如何根据花括号在vim中定义缩进?

我在 vim上使用 https://github.com/cakebaker/scss-syntax.vim语法突出显示SCSS(或SASS)文件,这非常适合语法高亮显示.但是,该插件没有附加缩进文件,并且在编写插件时遇到问题.

我想将缩进设置为如下所示:

但是,如果我做gg = G,我得到:

我怀疑它不理解基于大括号的嵌套缩进.我尝试了所有不同的组合

设置cindent

设定nocindent

设置自动注册

设置smartindent

并尝试使用Tab key == 4 spaces and auto-indent after curly braces in Vim代码,包括

设置tabstop = 2

设置shiftwidth = 2

设置expandtab

…但嵌套大括号缩进似乎永远不会起作用.

我相信我可能想写一个自定义缩进文件,我需要的是基于嵌套级别的大括号​​的缩进.我该怎么办呢?如果某人有一个具有类似语法的文件类型的缩进文件,那么这也是很好的.

这是一个快速入侵,基于内置的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,其中某些内容是您的文件类型(如果您在Windows上,请将〜/ .vim替换为vimfiles的路径.

您可能还想在文件的开头加上这个(但是只有在没有可能首先加载的其他缩进声明时):

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
    finish
endif
let b:did_indent = 1

相关文章

解决方案:解决linux下vim乱码的情况:(修改vimrc的内容)全...
Linuxvi/vim所有的UnixLike系统都会内建vi文书编辑器,其他的...
      vim正则匹配:空行:/^$/  /^[\t]*$/注释...
$select-editorSelectaneditor.Tochangelater,run'sele...
上次手贱忘了保存,这次就简单做个备忘吧,把踩过的坑记一下...
Linux之文本编译器小结vim的优势所有的UNIX-LIKE习通都会内置...