Vim 练习 - 录制和播放宏(macro)
关于宏
VIM 用户可以使用宏来录制编辑动作过程,然后再播放以减少重复操作。
具体命令如下:
qa:开始录制宏,并将宏保存到寄存器 a 中(你也可以把“a”想象成宏的名称),开始后会在状态栏显示“recording”
q:停止录制
@a :播放寄存器 a 中的宏
练习一
编辑前
# Appending text: The name "Vim" is an acronym for "Vi IMproved" The name "Vim" is an acronym for
Editing text:
Vim is a text editor originally released by Bram Moolenaar in 1991 for the Amiga
Trivia: Vim is a text editor released by Bram Moolenaar in 1991 for the Amiga
Deleting text:
Vim has a vi compatibility mode
Vim has a vi compatibility mode but when not in this mode Vim has many enhancements over vi
编辑后
将文件中的三对文本中的第二行都变得跟第一行一样。结果如下:
# Appending text: The name "Vim" is an acronym for "Vi IMproved" The name "Vim" is an acronym for "Vi IMproved"
Editing text:
Vim is a text editor originally released by Bram Moolenaar in 1991 for the Amiga
Vim is a text editor originally released by Bram Moolenaar in 1991 for the Amiga
Deleting text:
Vim has a vi compatibility mode
Vim has a vi compatibility mode
操作解释
qa 开始录制宏
j 下移一行
yy 复制一行
p 粘贴
j 下移一行
dd 删除一行
q 停止录制
2@a 播放两次
如果你这个比较简单,也没有什么实用价值,那就看下面这个吧。
练习二
编辑前
Annette
Warren
Anthony
Preston
Kelly
Taylor
Stiller
Dennis
Schwartz
编辑后
ALTER USER Annette IDENTIFIED BY 'Annette';
ALTER USER Warren IDENTIFIED BY 'Warren';
ALTER USER Anthony IDENTIFIED BY 'Anthony ';
ALTER USER Preston IDENTIFIED BY 'Preston';
ALTER USER Kelly IDENTIFIED BY 'Kelly ';
ALTER USER Taylor IDENTIFIED BY 'Taylor';
ALTER USER Stiller IDENTIFIED BY 'Stiller';
ALTER USER Dennis IDENTIFIED BY 'Dennis';
ALTER USER Schwart IDENTIFIED BY 'Schwart';
操作解释
q a: 开始录制宏,存储到寄存器 a 中
I “ALTER USER ”: 在行首插入“ALERT USER ”
Esc w yw:复制光标后的用户名
A “IDENTIFIED BY '”:到行尾输入 IDENTIFIED BY ‘
Esc p:粘贴之前复制的用户名
A “';”:在行尾关闭引号
Esc j q:下移一行并结束录制
8@a:播放宏 8 次
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论