加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Vim中对CSS文件进行字母排序

发布时间:2020-12-15 19:25:28 所属栏目:安全 来源:网络整理
导读:我得到一个CSS文件: 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; lin
我得到一个CSS文件:
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一次!
任何想法?谢谢!

: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

您当然可以将其映射到键盘快捷方式或将其转换为命令:

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

" Or:

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

那么你可以简单地点击F7或运行:

:SortCSSBraceContents

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读