当前位置: 首页 > 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.gsyq.cn/news/25603.html

相关文章:

  • Tuack 生成比赛题目 PDF 笔记
  • 4060显卡也能玩转AI改图!Flux.1 Kontext Dev GGUF版本超详细入门教程 - 实践
  • 提升生产力:8个.NET开源且功能强大的快速开发框架
  • 使用c++14标准实现函数注册包装
  • 【VSCode中Java创建环境安装的三个层级之Maven篇】(Windows版)
  • 2025年不锈钢酸洗钝化液厂家推荐排行榜,环保型不锈钢管酸洗钝化液,不锈钢清洗钝化液,酸洗钝化处理工艺及不锈钢清洗剂公司推荐
  • 2025年法兰保护罩厂家推荐排行榜,阀门保温罩,法兰罩,法兰防溅罩,法兰保护套,专业防护与定制服务优质供应商
  • 百度网盘非会员下载慢怎么解决 - fosgrignonhto
  • d435i 标定 imu和相机 用来复现vins_fusion - 教程
  • K230基础-摄像头的使用 - 详解
  • 2025年市面上高杆灯品牌Top10权威推荐榜单
  • Spring AOP 原理
  • 250921
  • Jvm参数分类
  • 10/20
  • 2025年市面上工程石材产品排名前十:选购指南与品牌深度解析
  • 2025年市面上工程石材产品排名前十:权威榜单与选择指南
  • 听说今年很多应届硕士很难找到工作...
  • 开源 C++ QT QML 创建(四)复杂控件--Listview
  • 意大利居留 办理 看小红书上的材料就行,部分材料可以到按手印再补交
  • 博客的意義
  • 我写过的动态规划问题的状态表示与转移汇总
  • 热点、排版、数据难题?6 款微信编辑器实测推荐
  • AI建的网站,真的对SEO友好吗?深度剖析其优势与潜在缺陷
  • 2025年上海律师推荐排行榜,经侦律师,民事纠纷律师,刑事律师,经济律师,婚姻律师,法务律师,负债律师事务所专业解析
  • 2025年棋牌室加盟品牌权威推荐榜:自主棋牌室加盟,自助棋牌室加盟,智能棋牌室加盟,共享棋牌室加盟品牌综合评测与选址运营指南
  • 2025年焊接变位机厂家权威推荐榜:变位机/双轴变位机专业制造,高精度传动与定制化解决方案实力解析
  • 20232426 2025-2026-1 《网络与系统攻防技术》实验二实验报告
  • 题单
  • 2025年防腐工程厂家最新权威推荐榜:喷砂/热喷锌/热喷铝/油漆涂装/热喷耐磨材料,专业工艺与长效防护解决方案