[VIM] reverse multiple lines in VIM

news/2025/10/20 20:40:10/文章来源:https://www.cnblogs.com/profesor/p/19153602

推荐方法:

If you’re on a Unix-like system (FreeBSD, Linux, macOS), use :14,19!tac.

 

来自chatgpt

 

To reverse the display order of lines 51 to 54 in Vim, you can use the :g and :tac-style command combination.

Here’s the most direct and idiomatic way to do it:

:51,54g/^/m51-1

Explanation

  • :51,54 → selects lines 51 through 54.

  • g/^/ → applies the following command (m51-1) to each line matching the regex ^ (i.e., every line in the range).

  • m51-1 → moves the current line just before line 51.
    Because Vim executes this for each line in top-to-bottom order, the sequence is reversed.

Step-by-step result

Suppose lines 51–54 originally are:

line51
line52
line53
line54

After executing the command, they become:

line54
line53
line52
line51

Notes

  • You can use :.,.+3g/^/m.-1 to do the same for “current line to current+3”. 这个方法,我尝试了下,没成功。

  • Always use line numbers or marks ('a, 'b) to ensure the correct range.

Would you like me to show a macro-based or visual-mode method too (for more interactive editing)?

 

Another good ways:

 

To reverse lines 14 through 19 in Vim (i.e., make line 19 become 14, 18 become 15, etc.), you can do this directly from Normal mode with a range and an Ex command.

Here’s the precise command:

:14,19!tac

Explanation:

  • : — enters command-line (Ex) mode.

  • 14,19 — specifies the line range (lines 14 through 19).

  • !tac — filters that range through the external Unix command tac, which reverses lines.

This will replace lines 14–19 with their reversed order.


If You’re on Windows (without tac):

Vim on Windows doesn’t have tac by default. You can simulate it using Vimscript:

:14,19g/^/m14

Explanation:

  • g/^/ — matches all lines (from the specified range).

  • m14 — moves each matched line after line 14, effectively reversing the block.

However: this command reverses relative to the top of the range, so if you start at line 14, it works cleanly.


Another Pure-Vim Method (portable across systems):

:14,19y a | 14,19d | put! a

Step-by-step:

  1. :14,19y a — yank (copy) lines 14–19 into register a.

  2. :14,19d — delete the original lines.

  3. :put! a — put (paste) them reversed (the ! means before the current line, reversing the order).

That’s the most reliable Vim-only way.


Recommendation:
If you’re on a Unix-like system (FreeBSD, Linux, macOS), use :14,19!tac.
If you want a Vim-only portable method, use the yank–delete–put trick.

Would you like me to give you a one-key macro that reverses any visually selected lines automatically?

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/941575.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DeviceNet 转 Ethernet/IP:三菱 Q 系列 PLC 与欧姆龙 CJ2M PLC 在食品饮料袋装生产线包装材料余量预警的通讯配置案例

案例背景 DeviceNet 转 Ethernet/IP在食品饮料行业,包装生产线涉及多种设备,如灌装机、贴标机、封口机等。不同设备可能采用不同的工业总线协议,为了实现整个包装生产线的自动化控制和数据共享,需要将采用 Etherne…

【大模型】【扫盲】几种不同的微调方法

四种微调方式Full 对预训练模型的所有参数进行微调,让模型从底层到顶层的所有参数都参与更新,彻底适配下游任务 优点:模型对任务的适配性最强,在数据充足、任务复杂时效果通常最优 缺点:资源消耗极大(千亿参数模…

Tuack 生成比赛题目 PDF 笔记

Part 1. Tuack 的基本介绍 Tuack,是一个由来自 THU 的大佬 Mulab11 开发的,用于造算法竞赛题目的工具。 你可以用它:导出 PDF、Markdown、HTML 等多种不同格式,NOI、CPC 等不同风格的题面。 简单的出题人自评测功能…

在 wrapper 类里实现重载方法

现有包装类 Wrapper 欲覆盖 inner 的方法 g class A{void f(){g();}void g(){} }class Wrapper extends A{A inner;void f(){inner.f() // 不调用 Wrapper.g}void g(){ // override g} }inner 通过 this.g 调用时仍然会…

Vue 项目 AI 文档增量更新工具操作手册

一、工具目标 通过 AI 自动生成 Vue 组件 / JS 工具的工程化解释文档,并支持增量更新(仅处理新增 / 修改的文件),避免重复劳动,提升团队文档效率。 二、环境准备前置条件 已初始化的 Vue 项目(Vue 2 或 Vue 3 均…

P7521 [省选联考 2021 B 卷] 取模 分析

题目概述 给你 \(n\) 个数 \(a_i\)。 求:\(\max_{i\ne j\ne k}(a_i+a_j)\bmod a_k\)。 分析 好题! 我一开始看到是无从下手的。 但是细想一下,关键点在于 \(a_k\),所以的说,枚举 \(a_k\) 是必不可少的。 然后我们…

4060显卡也能玩转AI改图!Flux.1 Kontext Dev GGUF版本超详细入门教程 - 实践

4060显卡也能玩转AI改图!Flux.1 Kontext Dev GGUF版本超详细入门教程 - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-fam…

提升生产力:8个.NET开源且功能强大的快速开发框架

提升生产力:8个.NET开源且功能强大的快速开发框架今天大姚给大家分享8个.NET开源、免费、功能强大的快速开发框架。助你提高开发生产效率、避免996!!! Vue.NetCore 一款基于Vue(提供Vue2/Vue3版本)和.Net Core前…

Mac版PDF Squeezer v4.5.1安装教程(DMG文件下载+详细步骤)​

Mac版PDF Squeezer v4.5.1安装教程(DMG文件下载+详细步骤)​​PDF Squeezer​ 是一款 ​Mac 专用​ 的 PDF 压缩工具,可以 ​快速减小 PDF 文件大小,同时尽量保持文件清晰度,适合 ​邮件发送、云端存储或节省空间…

使用c++14标准实现函数注册包装

调用方式bool res = FunctionRegistry::callFromFuncMap1<bool, type1&, type2*, type3,... >(...),其中第一个bool为返回值类型,第二个开始后面均为参数列表类型,可用于注册算子,包装函数指针等。 #inc…

【VSCode中Java创建环境安装的三个层级之Maven篇】(Windows版)

【VSCode中Java创建环境安装的三个层级之Maven篇】(Windows版)2025-10-20 20:15 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !imp…

黑马程序员Java基础笔记

目录类执行顺序继承方法重写构造器多态抽象类接口枚举类字符串内部类匿名内部类拆箱与装箱克隆浅克隆:深克隆:正则表达式Lambda方法引用静态方法引用实例方法引用特定类型方法引用构造器引用泛型通配符集合Collectio…

实用指南:linux磁盘空间爆满排查与清理

实用指南:linux磁盘空间爆满排查与清理pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Mo…

实用指南:socketpair深度解析:Linux中的“对讲机“创建器

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

详细介绍:从零开始的C++学习生活 2:类和对象(上)

详细介绍:从零开始的C++学习生活 2:类和对象(上)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas",…

【aigc】chrome-devtools-mcp怎么玩? - 指南

【aigc】chrome-devtools-mcp怎么玩? - 指南2025-10-20 20:02 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: b…

2025年不锈钢酸洗钝化液厂家推荐排行榜,环保型不锈钢管酸洗钝化液,不锈钢清洗钝化液,酸洗钝化处理工艺及不锈钢清洗剂公司推荐

2025年不锈钢酸洗钝化液厂家推荐排行榜:环保型不锈钢管酸洗钝化液技术解析与选购指南 行业背景与发展趋势 不锈钢材料因其优异的耐腐蚀性和美观性,在石油化工、食品医药、建筑装饰等领域得到广泛应用。随着环保政策的…

记账:流水报表

流水报表展示收入、支出、转账的流水条数,可用于判断记账的活跃状况(用处不大,由于只需在现有的设计上改动少许即可,因此顺手做了)查询流水报表:底部ledgers直接输入4(3表示出入报表),分组等其他条件可选下面…

2025年法兰保护罩厂家推荐排行榜,阀门保温罩,法兰罩,法兰防溅罩,法兰保护套,专业防护与定制服务优质供应商

2025年法兰保护罩厂家推荐排行榜:专业防护与定制服务优质供应商深度解析 在工业防护领域,法兰保护罩、阀门保温罩、法兰罩、法兰防溅罩、法兰保护套等产品已成为现代工业安全防护体系不可或缺的重要组成部分。随着工…