vim保存录制的宏并通过脚本非交互式应用

时间:2016-11-09 00:31:22

标签: vim

是否可以保存录制的vim宏并通过脚本非交互式应用该宏?

例如,假设我有100个类似的文件,如下所示:

variable number of lines
foo(a)
some more stuff
variable number of lines
bar(e)

在vim中,我可以录制和播放宏来将这些文件更改为:

variable number of lines
foo(a, b)
    some more stuff
    variable number of lines
bar(e)

宏看起来很难看,可能是这样的:

ESCqa/foo
/)
i, bESCj0
V/bar
k>ESCq

有没有办法保存该宏并以非交互方式应用它,例如所以它可以很容易地应用于100个文件,而无需在vim中交互式打开每个文件?

这是我已经尝试过的:

将我的宏保存为a,然后编写apply_a.ex脚本:

norm @a
w

最后ex target.txt < apply_a.ex

这不会更改文件内容 - 似乎无法识别宏a

2 个答案:

答案 0 :(得分:3)

来自:help starting

-w {scriptout}  All the characters that you type are recorded in the file
        "scriptout", until you exit Vim.  This is useful if you want
        to create a script file to be used with "vim -s" or
        ":source!".  When the "scriptout" file already exists, new
        characters are appended.  See also |complex-repeat|.
        {scriptout} cannot start with a digit.
        {not in Vi}

-s {scriptin}   The script file "scriptin" is read.  The characters in the
        file are interpreted as if you had typed them.  The same can
        be done with the command ":source! {scriptin}".  If the end
        of the file is reached before the editor exits, further
        characters are read from the keyboard.  Only works when not
        started in Ex mode, see |-s-ex|.  See also |complex-repeat|.
        {not in Vi}

这是你需要的吗?

答案 1 :(得分:1)

'抱歉没有正确格式化,我在Android' 你曾经使用'bufdo'或听说过吗? 您可以一次打开所有文件,并在宏中记录如下:bn,然后转到下一个。 Bufer和最后:墙意味着写所有,最后:qall

这里有一个例子:

或者您可以执行以下操作:

vim * 让@ a ='定义你的宏,包括:wn' 正常的@a :qall

其中*表示您需要的所有文件 :wn表示'写入当前缓冲区并继续下一步 n @a是宏a的n倍 :qall表示所有缓冲区enter image description here