gpt4 book ai didi

vim - 如何在Vim中按字母顺序排列CSS文件

转载 作者:行者123 更新时间:2023-12-03 12:26:41 24 4
gpt4 key购买 nike

我得到一个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

关于vim - 如何在Vim中按字母顺序排列CSS文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3050797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com