使用Vim,如何将CSS规则整合在一起? [英] Using Vim, how can I make CSS rules into one liners?

查看:34
本文介绍了使用Vim,如何将CSS规则整合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个Vim替换命令来转换多行CSS规则,就像这样:

I would like to come up with a Vim substitution command to turn multi-line CSS rules, like this one:

#main {
      padding: 0;
      margin: 10px auto;
}

转换为紧凑的单行规则,如下所示:

into compacted single-line rules, like so:

#main {padding:0;margin:10px auto;}

我有大量的CSS规则占用太多行,而且我无法弄清楚要使用的:%s/命令.

I have a ton of CSS rules that are taking up too many lines, and I cannot figure out the :%s/ commands to use.

推荐答案

这里是单线:

:%s/{\_.\{-}}/\=substitute(submatch(0), '\n', '', 'g')/

\ _.匹配任何字符,包括换行符,并且 \ {-} * 的非贪婪版本,因此 {\ _.\ {-}} 匹配一对匹配的花括号之间(包括两端)的所有内容.

\_. matches any character, including a newline, and \{-} is the non-greedy version of *, so {\_.\{-}} matches everything between a matching pair of curly braces, inclusive.

\ = 允许您替换vim表达式的结果,我们在这里使用该表达式从匹配的文本中去除所有换行符'\ n'(在 submatch(0)中使用 substitute()函数.

The \= allows you to substitute the result of a vim expression, which we here use to strip out all the newlines '\n' from the matched text (in submatch(0)) using the substitute() function.

逆(将单行版本转换为多行版本)也可以作为一个衬里来完成:

The inverse (converting the one-line version to multi-line) can also be done as a one liner:

:%s/{\_.\{-}}/\=substitute(submatch(0), '[{;]', '\0\r', 'g')/

这篇关于使用Vim,如何将CSS规则整合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆