如何按长度对行范围进行排序? [英] How do you sort a range of lines by length?

查看:57
本文介绍了如何按长度对行范围进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我只想按其长度在源文件和头文件的顶部对所有#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-" }'

这篇关于如何按长度对行范围进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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