如何按长度对一系列线进行排序?

时间:2022-08-07 21:38:55

Often I just want to sort all my #include's at the top of my source and header files by their length.

通常,我只想按源文件和头文件的长度对所有#include进行排序。

vim allows me to sort alphanumerically in a similar manner with :{range} sort u.

vim允许我以类似的方式对字母和数字排序:{range}排序u。

In vim, how do you sort a range of lines by the length of the line? Such that shorter lines are followed by longer lines.

在vim中,如何根据直线的长度对一系列直线进行排序?这样,更短的行之后是更长的行。

Searching the internet, I found this:

在网上搜索时,我发现:

:% s/.*/\=printf("%03d", len(submatch(0)))."|".submatch(0)/ | sor n | %s/..../

But that only works to sort the entire file, and is black magic to me anyway. I'm trying to figure out how to do that same sort with a range such as from line 4 to 18, as in :4,18 s/... Do you have any ideas?

但这只能对整个文件进行排序,这对我来说是一种黑魔法。我正试图弄明白如何对一个范围进行同样的排序,比如第4行到第18行,如:4,18 s/…你有什么想法吗?

3 个解决方案

#1


22  

Filter Visual Selection with Awk

One way to do this in vim is by filtering the visual selection with awk's length() function before sorting. For example:

在vim中实现这一点的一种方法是在排序之前使用awk的length()函数过滤视觉选择。例如:

:'<,'> ! awk '{ print length(), $0 | "sort -n | cut -d\\  -f2-" }'

#2


6  

One way, neither elegant nor efficient, but it works:

一种方式,既不优雅也不高效,但它起作用:

Add following function to your vimrc file. It inserts at the beginning of each line its number of characters, sort them numerically and deletes the numbers.

向vimrc文件中添加以下函数。它在每一行的开头插入它的字符数,对它们进行数字排序并删除数字。

function! SortLines() range
    execute a:firstline . "," . a:lastline . 's/^\(.*\)$/\=strdisplaywidth( submatch(0) ) . " " . submatch(0)/'
    execute a:firstline . "," . a:lastline . 'sort n'
    execute a:firstline . "," . a:lastline . 's/^\d\+\s//'
endfunction

Call it with a range of numbers, like

用一系列的数字来命名,比如

:4,18call SortLines()

or in Visual mode using V, like:

或者在视觉模式下使用V,比如:

:'<,'>call SortLines()

EDIT: Ops, now I realised that this solution is very similar to yours. It was fine, only that % means the complete buffer instead :4,18 or :'<,:'> that selects specific lines.

编辑:哦,现在我意识到这个解决方案和你的非常相似。可以,只是%表示完整的缓冲区,而不是:4、18或:'<,:'>,选择特定的行。

#3


4  

I've written the AdvancedSorters plugin to deal with these complex sorting requirements.

我已经编写了AdvancedSorters插件来处理这些复杂的排序要求。

Like in @Birei's answer, this plugin offers extension commands that evaluate an expression per line, put that number in front of the line, do a numerical sort, and then remove the temporary number again. Specializations handle the common sort by number of characters and by the line's display width, so you could just use:

就像@Birei的答案一样,这个插件提供了扩展命令来计算每行表达式的值,把这个数字放在一行的前面,进行数值排序,然后再次删除这个临时数字。专门化处理常用的字符数和行显示宽度,所以您可以使用:

:SortByWidth

#1


22  

Filter Visual Selection with Awk

One way to do this in vim is by filtering the visual selection with awk's length() function before sorting. For example:

在vim中实现这一点的一种方法是在排序之前使用awk的length()函数过滤视觉选择。例如:

:'<,'> ! awk '{ print length(), $0 | "sort -n | cut -d\\  -f2-" }'

#2


6  

One way, neither elegant nor efficient, but it works:

一种方式,既不优雅也不高效,但它起作用:

Add following function to your vimrc file. It inserts at the beginning of each line its number of characters, sort them numerically and deletes the numbers.

向vimrc文件中添加以下函数。它在每一行的开头插入它的字符数,对它们进行数字排序并删除数字。

function! SortLines() range
    execute a:firstline . "," . a:lastline . 's/^\(.*\)$/\=strdisplaywidth( submatch(0) ) . " " . submatch(0)/'
    execute a:firstline . "," . a:lastline . 'sort n'
    execute a:firstline . "," . a:lastline . 's/^\d\+\s//'
endfunction

Call it with a range of numbers, like

用一系列的数字来命名,比如

:4,18call SortLines()

or in Visual mode using V, like:

或者在视觉模式下使用V,比如:

:'<,'>call SortLines()

EDIT: Ops, now I realised that this solution is very similar to yours. It was fine, only that % means the complete buffer instead :4,18 or :'<,:'> that selects specific lines.

编辑:哦,现在我意识到这个解决方案和你的非常相似。可以,只是%表示完整的缓冲区,而不是:4、18或:'<,:'>,选择特定的行。

#3


4  

I've written the AdvancedSorters plugin to deal with these complex sorting requirements.

我已经编写了AdvancedSorters插件来处理这些复杂的排序要求。

Like in @Birei's answer, this plugin offers extension commands that evaluate an expression per line, put that number in front of the line, do a numerical sort, and then remove the temporary number again. Specializations handle the common sort by number of characters and by the line's display width, so you could just use:

就像@Birei的答案一样,这个插件提供了扩展命令来计算每行表达式的值,把这个数字放在一行的前面,进行数值排序,然后再次删除这个临时数字。专门化处理常用的字符数和行显示宽度,所以您可以使用:

:SortByWidth