保存 vim 宏

发布于 2024-08-17 07:22:09 字数 34 浏览 2 评论 0原文

有谁知道如何正确保存/重用 vim 编辑器中记录的宏?

Does anyone know how to properly save/reuse macros recorded inside of a vim editor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无尽的现实 2024-08-24 07:22:09

使用q 后跟一个字母(例如“x”)来录制宏,然后按q 结束宏定义。在此定义期间,由开始和结束 'q' 包围的模式仅进入复制/粘贴寄存器之一(在本例中为寄存器“x”),因此您可以使用粘贴它> 正常模式下的"xp"xP 命令,其中 x 是要粘贴的寄存器。在正常模式下键入 "xp 会将内容插入到寄存器 x 中,然后退出回到正常模式。

要保存它,您可以打开 .vimrc 并粘贴内容,同时使用 let @x 定义宏,那么下次启动 vim 时寄存器就会出现。
格式类似于:

let @q = 'macro contents'

不过要小心引号。他们必须得妥善逃脱。

因此,要保存宏 'x',您可以执行以下操作:

  • 在正常模式下: qx
  • 输入任何命令
  • 在正常模式下: q
  • open .vimrc
  • 插入一行 let @x = '...' (见下文)
  • 对于上面的 ... 模式,您可以使用 "xp > 就在应该放置模式的地方,但这不是必需的,您可以直接键入宏定义。

Use q followed by a letter (for example 'x') to record a macro, then press q 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:

let @q = 'macro contents'

Be careful of quotes, though. They would have to be escaped properly.

So to save a macro 'x', you can do:

  • From normal mode: qx
  • enter whatever commands
  • From normal mode: q
  • open .vimrc
  • insert a line let @x = '...' (see the following)
  • For the above ... 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.
如歌彻婉言 2024-08-24 07:22:09

要获得更强大的解决方案,您可以查看 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.

如梦 2024-08-24 07:22:09

将宏写入 ~/.vimrc 中,例如要定义由 CTRL+O 启动的宏,请将以下行添加到 ~/.vimrc 中:

map <C-O> MACROTEXT

当您通过输入 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 :

map <C-O> MACROTEXT

when you record a macro by typing qa you can retrieve your macro text by typing "ap

别挽留 2024-08-24 07:22:09

您可以在 ~/.vimrc 上执行此操作

:let @a="iHello World!\<CR>bye\<Esc>"

注意:您必须使用双引号才能使用 \ 中的特殊键。

You can do like this on your ~/.vimrc

:let @a="iHello World!\<CR>bye\<Esc>"

NOTE: You must use double quotes to be able to use special keys like in \<this silly example>.

肤浅与狂妄 2024-08-24 07:22:09

: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.

故事灯 2024-08-24 07:22:09

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文