当前位置: 首页 > 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.hskmm.com/?act=detail&tid=35278

相关文章:

  • Vue 项目 AI 文档增量更新工具操作手册
  • 4060显卡也能玩转AI改图!Flux.1 Kontext Dev GGUF版本超详细入门教程 - 实践
  • 记账:流水报表
  • 2025年法兰保护罩厂家推荐排行榜,阀门保温罩,法兰罩,法兰防溅罩,法兰保护套,专业防护与定制服务优质供应商
  • 英伟达微型AI工作站的架构解析与性能突破
  • 题解 QOJ 7766 [集训队互测 2023] 栞
  • 遥感的基本概念
  • d435i 标定 imu和相机 用来复现vins_fusion - 教程
  • 20232418 2025-2026-1 《网络与系统攻防技术》实验二实验报告
  • CF1777E Edge Reverse
  • CSP-S 模拟赛 Day 19
  • CSP-S 模拟赛 Day 18
  • 2025年锥芯板品牌口碑排行榜单Top10:行业精选与选择指南
  • 2025年给汤机/重力铸造自动化/机加工自动化厂家推荐榜单:专业设备与智能解决方案权威解析
  • 2025年发电机厂家权威推荐榜:柴油发电机组/康明斯/玉柴/高压/大功率发电机组专业选购指南
  • 强网杯s9初赛 PolyEncryption wp
  • 基于TPS5450DDAR的24V转12V降压电路设计
  • 【STM32项目开源】基于STM32的智能宠物防丢监控便捷的系统
  • 20232409 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 训高代
  • Spring AOP 原理
  • 详细介绍:医疗人读懂 LLM 的第二课: 使用 Transformer 下篇
  • 250921
  • P11233 [CSP-S 2024] 染色题解
  • 位运算(早晚得学会)
  • Jvm参数分类
  • 10/20
  • 2025年市面上工程石材产品排名前十:选购指南与品牌深度解析
  • 2025年市面上工程石材产品排名前十:权威榜单与选择指南
  • 意大利居留 办理 看小红书上的材料就行,部分材料可以到按手印再补交