Vim 中 CSS 的缩进烦恼

发布于 2024-08-26 18:49:14 字数 648 浏览 8 评论 0原文

我最近从 TextMate 迁移到 Vim,并且我真的很喜欢这种切换。然而,我对 Vim 使用 CSS 语法处理大括号内缩进的方式感到渴望。我使用 simple_pairs.vim,它可能与我的问题有关,也可能没有,但我不这么认为,因为在 PHP、JavaScript 等中一切正常。让我解释一下……

我通常按上下文对 CSS 规则进行分组使用缩进,如下所示:

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

这意味着当我键入 ul#nav li 规则时,后跟 { (自动插入相应的 })然后按 Enter 键,我希望右大括号与 ul#... 具有相同的缩进级别,但我得到的是这样的结果:

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

所以我必须手动缩进额外的步骤。就像我说的,在 PHP、JavaScript 等中做同样的事情,效果很好。有谁知道我该如何解决这个问题?我对 Vim 的语法定义文件了解不够,无法弄清楚 PHP 语法文件中的内容使其起作用,并将其移植到 CSS 中……谢谢。

I've moved from TextMate to Vim lately, and am really liking the switch. However, I have an itch regarding the way Vim handles indentation within curly braces using the CSS syntax. 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…

I generally group my CSS rules by context using indentation, like so:

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 {
  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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-09-02 18:49:14

我在这里找到了一个非常好的嵌套大括号缩进代码:

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

你可以将它保存在 .vim/indent/css.vim 中,它会比默认的 CSS 缩进效果更好。

此外,它也适用于 .less 文件,但您可能必须在 .vimrc 文件中将它们与它关联。

github 上的脚本与官方 css.vim 的维护者相同,仅晚了 6 年。他们有几条不同的线路。

I found a very good indent code for nested curly brackets here:

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.

Also, it works for .less files as well, but you may have to associate those to it in your .vimrc file.

The script at github is by the same mantainer as the official css.vim, only 6 years newer. They have a few different lines.

九公里浅绿 2024-09-02 18:49:14

我对 css 文件执行了此操作:

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

我没有设置 smartindent,但您也可以添加它。

这表示,当您进入 .css 文件的缓冲区时,您应该取消设置 cindent,并在离开缓冲区时将其设置回来。

I did this for css files:

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

I didn't have smartindent set, but you could add that as well.

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.

慵挽 2024-09-02 18:49:14

这与 cindent 和 smartindent 有关。其中之一会导致这种行为。

我忘记了是哪一个(也许是两个?),但我从我的 ~/.vimrc 文件中删除了它们,只保留了 autoindent

This has to do with cindent and smartindent. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文