How to define indents in vim based on curly braces?

I use https://github.com/cakebaker/scss-syntax.vim to highlight the syntax of the SCSS (or SASS ) files on vim, which works great for syntax highlighting. However, the plugin does not come with indentation, and I am having trouble writing.

I would like to indent this way:

enter image description here

However, if I do gg=G , I get:

enter image description here

I suspect that he does not understand the nested indentation based on curly braces. I tried all the different combinations

set cindent

set nocindent

set autoindent

set smartindent

and tried using code from Tab key == 4 spaces and auto-indent after curly braces in Vim , including

set tabstop=2

set shiftwidth=2

set expandtab

... but the nested indentation set never works.

I believe that I can write my own indent file, and all I need is padding based on curly brackets with nested levels. How can I do it? If someone has an indent file for file types with similar syntax, that would be fine too.

+6
vim indentation sass auto-indent
source share
1 answer

This is a quick hack based on the perl embedded embed code (in indent/perl.vim ). Hope you can use it to get what you want. See the more detailed comments in the perl indent code or in another file in the indent directory for more details.

 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 

Save this file as ~/.vim/indent/something.vim , where something is your file type (replace ~/.vim with the vimfiles path if you are on Windows.

You may also want to write this at the beginning of the file (but only if it did not have another ad with a retreat):

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

All Articles