当前位置: 首页 > news >正文

[VIM] reverse multiple lines in VIM

推荐方法:

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.jsqmd.com/news/17769/

相关文章:

  • DeviceNet 转 Ethernet/IP:三菱 Q 系列 PLC 与欧姆龙 CJ2M PLC 在食品饮料袋装生产线包装材料余量预警的通讯配置案例
  • 【大模型】【扫盲】几种不同的微调方法
  • Tuack 生成比赛题目 PDF 笔记
  • 在 wrapper 类里实现重载方法
  • Vue 项目 AI 文档增量更新工具操作手册
  • P7521 [省选联考 2021 B 卷] 取模 分析
  • 4060显卡也能玩转AI改图!Flux.1 Kontext Dev GGUF版本超详细入门教程 - 实践
  • 提升生产力:8个.NET开源且功能强大的快速开发框架
  • Mac版PDF Squeezer v4.5.1安装教程(DMG文件下载+详细步骤)​
  • 使用c++14标准实现函数注册包装
  • 【VSCode中Java创建环境安装的三个层级之Maven篇】(Windows版)
  • 详细揭秘:马拉车算法
  • 黑马程序员Java基础笔记
  • 实用指南:linux磁盘空间爆满排查与清理
  • 实用指南:socketpair深度解析:Linux中的“对讲机“创建器
  • 详细介绍:从零开始的C++学习生活 2:类和对象(上)
  • 【aigc】chrome-devtools-mcp怎么玩? - 指南
  • 2025年不锈钢酸洗钝化液厂家推荐排行榜,环保型不锈钢管酸洗钝化液,不锈钢清洗钝化液,酸洗钝化处理工艺及不锈钢清洗剂公司推荐
  • 记账:流水报表
  • 百度网盘非会员下载慢怎么解决 - fosgrignonhto
  • 嵌入式硬件——基于IMX6ULL的UART(通用异步收发传输器) - 教程
  • 20232418 2025-2026-1 《网络与系统攻防技术》实验二实验报告
  • CF1777E Edge Reverse
  • d435i 标定 imu和相机 用来复现vins_fusion - 教程
  • CSP-S 模拟赛 Day 19
  • K230基础-摄像头的使用 - 详解
  • CSP-S 模拟赛 Day 18
  • 2025年市面上高杆灯品牌前十强推荐榜:选购指南与行业洞察
  • 2025年市面上高杆灯品牌Top10权威推荐榜单
  • 2025年国内高杆灯十大品牌权威推荐榜单