通常,我只想按其长度在源文件和头文件的顶部对所有#include进行排序.
Often I just want to sort all my #include's at the top of my source and header files by their length.
vim允许我以与:{range} sort u类似的方式对字母数字进行排序.
vim allows me to sort alphanumerically in a similar manner with :{range} sort u.
在vim中,如何按行长对行范围进行排序?这样,较短的行之后便是较长的行.
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.
在互联网上搜索,我发现了这一点:
Searching the internet, I found this:
:% s/.*/\=printf("%03d", len(submatch(0)))."|".submatch(0)/ | sor n | %s/..../但这只能对整个文件进行排序,无论如何对我来说是不可思议的.我正在尝试找出如何在范围从第4行到第18行的范围内执行相同的排序,如:4,18 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?
推荐答案使用Awk过滤视觉选择
在vim中执行此操作的一种方法是在排序之前用awk的length()函数过滤视觉选择.例如:
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:
:'<,'> ! awk '{ print length(), $0 | "sort -n | cut -d\\ -f2-" }'更多推荐
如何按长度对行范围进行排序?
发布评论