我有一个如下文件
a
b
c
d
e
f
g
h
i
j
k
Run Code Online (Sandbox Code Playgroud)
我希望它是:
a
e
i
Run Code Online (Sandbox Code Playgroud)
在vim中如何做?
您可以按照以下按键顺序进行操作:
gg
qa
j
3dd
q
999@a
Run Code Online (Sandbox Code Playgroud)
这是命令的含义:
gg # jump to the beginning of the file
qa # start recording a macro in register "a"
j # move down 1 line
3dd # delete 3 lines (including the current one)
q # stop recording the macro
999@a # replay the macro from register "a"
Run Code Online (Sandbox Code Playgroud)
如果您sed的系统中装有Linux(Mac OS X,或者在Windows中使用Git Bash),那么这样做比较干净:
:%!sed -ne 1~4p
Run Code Online (Sandbox Code Playgroud)
这将过滤整个缓冲区的内容,并将其发送到sed管道,然后sed命令从第1行开始打印输入的每第4行。