简体   繁体   English

在Vim中使用CSS缩进烦恼

[英]Indentation annoyance with CSS in Vim

I've moved from TextMate to Vim lately, and am really liking the switch. 我最近从TextMate搬到了Vim,我真的很喜欢这个开关。 However, I have an itch regarding the way Vim handles indentation within curly braces using the CSS syntax. 但是,关于Vim使用CSS语法处理花括号中的缩进的方式,我有一个痒。 I use simple_pairs.vim, which may or may not have something to do with my problem, but I don't think so, as things work fine in PHP, JavaScript, etc. Let me explain… 我使用的是simple_pairs.vim,它可能与我的问题有关,也可能没有,但我不这么认为,因为PHP,JavaScript等工作正常。让我解释一下......

I generally group my CSS rules by context using indentation, like so: 我通常使用缩进按上下文对CSS规则进行分组,如下所示:

ul#nav {
  margin: 10px;
}
  ul#nav li {
    float: left;
    margin-right: 4px;
  }

That means when I type my ul#nav li rule, followed by { (which inserts a corresponding } automatically) and hit enter, I want the closing brace to be at the same indentation level as the ul#… , but instead I get something like this: 这意味着当我输入我的ul#nav li规则,然后是{ (自动插入相应的} )并按Enter键时,我希望右括号与ul#…处于同一缩进级别,但我得到了一些东西像这样:

ul#nav {
  margin: 10px;
}
  ul#nav li {
}

So I have to indent the extra step(s) manually. 所以我必须手动缩进额外的步骤。 Like I said, doing the same thing in PHP, JavaScript, etc, works fine. 就像我说的,在PHP,JavaScript等中做同样的事情,效果很好。 Does anyone know how I can fix this? 有谁知道我怎么解决这个问题? I don't understand enough of Vim's syntax definition files for me to be able to figure out what in the PHP syntax file makes it work, and port it over to the CSS one… Thanks. 我不太了解Vim的语法定义文件,因为我能够弄清楚PHP语法文件中的哪些内容使其工作,并将其移植到CSS ...谢谢。

I found a very good indent code for nested curly brackets here: 我在这里找到了一个非常好的嵌套花括号缩进代码:

https://gist.github.com/762326/bcbd35239db7f26447f1c2323037d20a5219471d https://gist.github.com/762326/bcbd35239db7f26447f1c2323037d20a5219471d

You can save it in .vim/indent/css.vim and it'll do a much better job of indenting CSS than the default. 您可以将它保存在.vim / indent / css.vim中,它可以比默认情况下更好地缩进CSS。

Also, it works for .less files as well, but you may have to associate those to it in your .vimrc file. 此外,它也适用于.less文件,但您可能必须在.vimrc文件中将它们与它相关联。

The script at github is by the same mantainer as the official css.vim, only 6 years newer. github上的脚本与官方css.vim的脚本相同,只有6年的新版本。 They have a few different lines. 他们有几条不同的路线。

I did this for css files: 我为css文件做了这个:

au BufEnter *.css set nocindent
au BufLeave *.css set cindent

I didn't have smartindent set, but you could add that as well. 我没有smartindent设置,但你也可以添加它。

This says that when you enter a buffer of a .css file, you should unset cindent , and you should set it back when you leave the buffer. 这表示当你输入.css文件的缓冲区时,你应该取消设置cindent ,你应该在离开缓冲区时将其设置回来。

This has to do with cindent and smartindent . 这与cindentsmartindent One of them causes this behavior. 其中一个导致此行为。

I forgot which one (maybe both of them?), but I removed them from my ~/.vimrc file, and kept only autoindent 我忘了哪一个(也许他们两个?),但我删除它们从我~/.vimrc文件,并只保留autoindent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM