使用 Vim,如何将 CSS 规则整合为一个衬垫? [英] Using Vim, how can I make CSS rules into one liners?
问题描述
我想出一个 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), '
', '', '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 表达式的结果,我们在这里使用它从匹配的文本中去除所有换行符 '
'
(在 submatch(0)
) 中使用 substitute()
函数.
The =
allows you to substitute the result of a vim expression, which we here use to strip out all the newlines '
'
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), '[{;]', '
', 'g')/
这篇关于使用 Vim,如何将 CSS 规则整合为一个衬垫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!