如何在 Vim 中按字母顺序排列 CSS 文件 [英] How to Alphabetize a CSS file in Vim

查看:37
本文介绍了如何在 Vim 中按字母顺序排列 CSS 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个 CSS 文件:

I get a CSS file:

div#header h1 {
    z-index: 101;
    color: #000;
    position: relative;
    line-height: 24px;
    margin-right: 48px;
    border-bottom: 1px solid #dedede;
    font-size: 18px;
}

div#header h2 {
    z-index: 101;
    color: #000;
    position: relative;
    line-height: 24px;
    margin-right: 48px;
    border-bottom: 1px solid #dedede;
    font-size: 18px;
}

我想在 {...}

div#header h1 {
    border-bottom: 1px solid #dedede;
    color: #000;
    font-size: 18px;
    line-height: 24px;
    margin-right: 48px;
    position: relative;
    z-index: 101;
}

div#header h2 {
    border-bottom: 1px solid #dedede;
    color: #000;
    font-size: 18px;
    line-height: 24px;
    margin-right: 48px;
    position: relative;
    z-index: 101;
}

我映射F7来做

nmap <F7> /{/+1<CR>vi{:sort<CR>

但我需要一遍又一遍地按 F7 才能完成工作.
如果 CSS 文件很大,就很费时间 &很容易感到无聊.
我想通过管道传输 cmds.所以,我只按一次 F7
任何的想法?谢谢!

But I need to press F7 over and over again to get the work done.
If the CSS file is big, It's time-consuming & easily get bored.
I want to get the cmds piped. So that, I only press F7 once!
Any idea? thanks!

推荐答案

:g#\({\n\)\@<=#.,/}/sort

说明:

g        " Work over the whole file running .,/}/sort on each line that matches
         " the pattern \({\n\)\@<=
#...#... " Delimiters: first bit is search pattern, second bit is what
         " to do on each matching line
\(       " Grouping, containing:
  {\n    " Open brace followed by new line
\)       " End of grouping
\@<=     " Negative look-behind, so match after the new-line, but make sure that
         " the match point is preceded by an open brace and a new-line

.,/}/    " From this line to the next closing brace...
sort     " Sort the lines

您当然可以将其映射到键盘快捷键或使其成为命令:

You can of course map this to a keyboard shortcut or make it into a command:

:nmap <F7> :g#\({\n\)\@<=#.,/}/sort<CR>

" Or:

:command! SortCSSBraceContents :g#\({\n\)\@<=#.,/}/sort

然后你可以简单地点击 F7 或运行:

Then you can simply hit F7 or run:

:SortCSSBraceContents

这篇关于如何在 Vim 中按字母顺序排列 CSS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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