惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 陌上花kai

openclaw实践 RooCode结合NacosMCP实践 Dify0.15.1升级1.4.3版本 RooCode结合本地MCP实践 本地密码管理器-Vaultwarden 从业务视角看AI落地:避免技术狂欢,聚焦真实需求 从"我要去山西旅游"看AI智能体的未来之旅:大模型如何重构自动化服务生态 OpenManus+DeepSeek体验 企业知识库落地实践 搭建个人AI知识库-DIFY 搭建个人AI知识库:RAG与本地模型实践指南 分享一个前后端分离后台管理系统 CV高手是怎么炼成的? linux下自建NAS教程 一个9年archlinux重度使用者自述 在日常工作和生活中使用Linux-开篇 springboot集成测试最小化依赖实践 ChatGpt怎么玩 如何通过电脑手柄玩安卓手游?
vim系列-文本操作篇
陌上花kai · 2024-08-30 · via 博客园 - 陌上花kai

基数行与偶数行分组

使用Vim的替换命令,可以轻松地将基数行和偶数行分组:

%s/\(^.*$\)\n\(^.*$\)/\1 \2/g

然后,删除所有的基数行:

%s/^.*$\n\(^.*$\)/\1/g

删除重复行

在Vim中删除重复行是一个常见的操作,以下是几种方法:

删除相邻重复行

:g/\(.\+\)$\n\1/d

删除不相邻重复行

使用排序命令删除不相邻的重复行:

:sort u

删除重复行,结果按照原顺序排列

为了保存原有顺序,首先给每行加上行号和1个{

:let i=1|g/^/s//\=i.'{'/|let i+=1

按照行号后面的内容排序:

:sort /^\d\{-}/

删除行号后面的内容相同的行保留后面的行:

:g/^\d\{-}\{.∗$\n\d\{-}\{1$/d

按照行号恢复顺序:

:sort n

删除空白行

删除文件中的空白行:

%g/^\s*$/d

添加序号

有多种方法可以为文本添加序号:

通过let变量

let i=1 | g/^/s//\=i.' '/ | let i=i+1

直接使用行号

:g/^/ s//\=line('.').' '

使用range()函数

:for i in range(1, 31)
:  call setline(i, i .' '. getline(i))
:endfor

利用Vim的编程支持

:python <<EOF
from vim import current
for i in range(len(current.buffer)):
current.buffer[i] = str(i+1) + ' ' + current.buffer[i]
EOF

外部命令

使用外部命令如findstr, sed, diff, perl, python等为文本添加序号:

:%!findstr /N "^"
:%!sed =|sed "N;s/\n/ /"
:%!diff --line-format=%dn%L % -
:%!perl -pe "print ++$a . ' '"
:%!python -c "import sys,fileinput as f;[sys.stdout.write(str(f.lineno())+a) for a in f.input()]"

查看文件行数、列数、字符数及所占字节大小

查看文件的行数、列数、字符数及所占字节大小:

g + <Ctrl-g>