Vim 宏实战操作

宏的概念

什么是宏呢?英文名:macro,代表一串命令的集合。

示例操作文本

SELECT * FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`userType`=3)) AND (`status` >= 0) AND (`correctCnt` > 0) ORDER BY `addTime` DESC LIMIT 16 OFFSET 30;
SELECT `imgId`, `taskId`, `imgUrl`, `status`, `ocrResult`, `feedback`, `totalQuestionCount`, `correctQuestionCount`, `finalOcrResult`, `addTime`, `completeTime`, `updateTime` FROM `edu_ocr_img` WHERE (`taskId` IN ('143736', '143725', '143723', '143722', '143715', '143682', '143681', '143677', '143676', '143651')) AND (`status` > 0);
SELECT sum(correctCnt) FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`userType`=3)) AND (`status` >= 0);
UPDATE `edu_ocr_task` SET `status`=-2 WHERE (`userId`=11580815) AND (`status` IN (1, 0)) AND (`userType`=1) AND (`taskId`=224876);
UPDATE `edu_ocr_task` SET `status`=-2 WHERE (`userId`=11580815) AND (`status` IN (1, 0)) AND (`userType`=1);

. 号宏

. 号是英文中的句号.
需要在普通模式中执行.
执行效果为: 重复执行一次上次的修改 (关于修改的定义不做详细解析).

命令序列: cwaaa j.

结果:
(注意 aaa 的位置)

aaa * FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`userType`=3)) AND (`status` >= 0) AND (`correctCnt` > 0) ORDER BY `addTime` DESC LIMIT 16 OFFSET 30;
SEaaa `imgId`, `taskId`, `imgUrl`, `status`, `ocrResult`, `feedback`, `totalQuestionCount`, `correctQuestionCount`, `finalOcrResult`, `addTime`, `completeTime`, `updateTime` FROM `edu_ocr_img` WHERE (`taskId` IN ('143736', '143725', '143723', '143722', '143715', '143682', '143681', '143677', '143676', '143651')) AND (`status` > 0);
SELECT sum(correctCnt) FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`userType`=3)) AND (`status` >= 0);
UPDATE `edu_ocr_task` SET `status`=-2 WHERE (`userId`=11580815) AND (`status` IN (1, 0)) AND (`userType`=1) AND (`taskId`=224876);
UPDATE `edu_ocr_task` SET `status`=-2 WHERE (`userId`=11580815) AND (`status` IN (1, 0)) AND (`userType`=1);

录制宏

q 键既是录制的开始, 也是录制的结束按键. 录制过程中会显示recording @{字母}

示例按键: qqdwjq@q@@@@

将每一行的第一个单词删除:

* FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`userType`=3)) AND (`status` >= 0) AND (`correctCnt` > 0) ORDER BY `addTime` DESC LIMIT 16 OFFSET 30;
`imgId`, `taskId`, `imgUrl`, `status`, `ocrResult`, `feedback`, `totalQuestionCount`, `correctQuestionCount`, `finalOcrResult`, `addTime`, `completeTime`, `updateTime` FROM `edu_ocr_img` WHERE (`taskId` IN ('143736', '143725', '143723', '143722', '143715', '143682', '143681', '143677', '143676', '143651')) AND (`status` > 0);
sum(correctCnt) FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`userType`=3)) AND (`status` >= 0);
`edu_ocr_task` SET `status`=-2 WHERE (`userId`=11580815) AND (`status` IN (1, 0)) AND (`userType`=1) AND (`taskId`=224876);
`edu_ocr_task` SET `status`=-2 WHERE (`userId`=11580815) AND (`status` IN (1, 0)) AND (`userType`=1);

示例讲解

qq : q 开启录制宏, 选择 q 这个寄存器放置宏的内容
dwjq : 删除第一个单词, 并且光标下移一行, q 结束录制
@q : 回放存放在 q 寄存器里面的命令
@@: 回放上一次执行的宏

这样一个简单的宏就完成了.

查看宏的内容

:reg q
会显示如下内容:

:reg q
--- Registers ---
"q   dwj         

修改宏

在录制过程中, 难免会录制错误. 那么如何修改你已经录制好的宏呢?

在宏的尾部增加命令

qQ 这回将你之后输入的序列追加到 q 寄存器中, 而不是重新覆盖.

重新编辑宏指令

  1. :tabn | 重新打开一个 tab
  2. :put q | 将 q 寄存器里面的内容复制到当前文件中
  3. 编辑你的宏命令 (在编辑之前有必要先去学习一下宏中的键盘编码)
  4. :d q | 将当前行删除, 并且放到 q 寄存器中
  5. @q | 执行 q 寄存器中的指令

你可能感兴趣的:(Vim 宏实战操作)