保存 vim 宏
有谁知道如何正确保存/重用 vim 编辑器中记录的宏?
Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有谁知道如何正确保存/重用 vim 编辑器中记录的宏?
Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
使用
q
后跟一个字母(例如“x”)来录制宏,然后按q
结束宏定义。在此定义期间,由开始和结束'q'
包围的模式仅进入复制/粘贴寄存器之一(在本例中为寄存器“x”),因此您可以使用粘贴它> 正常模式下的"xp
或"xP
命令,其中 x 是要粘贴的寄存器。在正常模式下键入"xp
会将内容插入到寄存器 x 中,然后退出回到正常模式。要保存它,您可以打开 .vimrc 并粘贴内容,同时使用
let @x 定义宏
,那么下次启动 vim 时寄存器就会出现。格式类似于:
不过要小心引号。他们必须得妥善逃脱。
因此,要保存宏
'x'
,您可以执行以下操作:qx
q
let @x = '...'
(见下文)...
模式,您可以使用"xp
> 就在应该放置模式的地方,但这不是必需的,您可以直接键入宏定义。Use
q
followed by a letter (for example 'x') to record a macro, then pressq
to end the macro definition. The pattern surrounded by the starting and ending'q'
during this definition just goes into one of the copy/paste registers (in this case, register 'x') so you can paste it with the"xp
or"xP
commands in normal mode, where x is the register to paste. Typing"xp
in normal mode inserts the contents in register x and exits back to normal mode.To save it, you open up .vimrc and paste the contents while defining the macro using
let @x
, then the register will be around the next time you start vim.The format is something like:
Be careful of quotes, though. They would have to be escaped properly.
So to save a macro
'x'
, you can do:qx
q
let @x = '...'
(see the following)...
pattern, you can use"xp
just at the place where the pattern should be placed. But this is not essential, you can just type in the macro definition.要获得更强大的解决方案,您可以查看 Marvim。
它允许您将宏保存在特定的命名空间中(或使用文件类型作为默认命名空间),并且您可以稍后搜索保存的宏并将它们加载到准备使用的寄存器中。
如果您重复使用很多宏,这非常有帮助。
For a more robust solution you can checkout Marvim.
It lets you save a macro in a specific namespace (or use the filetype as a default namespace) and you can later search for your saved macros and load them in a register ready for use.
If you reuse a lot of macros, this is pretty helpful.
将宏写入 ~/.vimrc 中,例如要定义由 CTRL+O 启动的宏,请将以下行添加到 ~/.vimrc 中:
当您通过输入
qa
录制宏时,您可以通过输入"ap
检索宏文本Write your macros inside your ~/.vimrc, to define a macro launched by CTRL+O by example, add the following line to your ~/.vimrc :
when you record a macro by typing
qa
you can retrieve your macro text by typing"ap
您可以在
~/.vimrc
上执行此操作注意:您必须使用双引号才能使用
\
中的特殊键。You can do like this on your
~/.vimrc
NOTE: You must use double quotes to be able to use special keys like in
\<this silly example>
.:mkexrc
(或:mkvimrc
) 命令可用于保存当前所有的:map
和:set
设置到文件。有关详细信息,请参阅:help mkexrc
。The
:mkexrc
(or:mkvimrc
) command can be used to save all the current:map
and:set
settings to a file. See:help mkexrc
for details.MacOS Mojave (10.14.6) 上的 Vim 8.0 实际上会自动保留宏和命名缓冲区(默认情况下,尽管我还没有寻找关闭此行为的方法)。关闭 Vim 会话将使用任何命名的缓冲区/宏更新
~/.viminfo
文件。Vim 8.0 on MacOS Mojave (10.14.6) actually persists macros and named buffers automatically (by default, although I haven't looked for a way of turning this behavior off). Closing a Vim session will update the
~/.viminfo
file with any named buffers / macros.