如何按长度对行范围进行排序?

编程入门 行业动态 更新时间:2024-10-09 21:19:18
本文介绍了如何按长度对行范围进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何按长度对行范围进行排序?

本文发布于:2023-11-29 09:01:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!