Append keys to an earlier recording in vim
Asked Answered
R

2

10

I know what recording in vim is.

Let's say I have already a recording on a register and I'm missing some more keys that I want to add to that sequence. Is there a way to append these keys to an earlier recording in vim?

Ragwort answered 21/10, 2018 at 11:15 Comment(0)
H
24

Using an uppercase letter will append to a register, so qA would continue recording the @a macro.

From :help q:

q{0-9a-zA-Z"}       Record typed characters into register {0-9a-zA-Z"}
                    (uppercase to append).

Note: this works for everything related to registers, so "Ayw would also append the next word to register "a.

Hutto answered 21/10, 2018 at 11:37 Comment(0)
Z
3

Pasting the Macro

You can paste your register on the current file by doing this (let's say register "a"):

:put a

If I want to delete the last word of the line and I want to erase the space before it, jump to the beginning of the line and go down one line

Original register "a":

$diw

Pasting the register

:put a

On insert mode we need insert the macro literally, so we need to use Ctrl-rCtrl-ra

Note: The second Ctrl-r is necessary to capture Esc or Enter.

Modifying the Macro

Modifying the register "a":

$diwx0j

In the above example we just added x0j

Reassigning the Macro

Then you can make a visual selection and yank to the register "a":

0vg_"ay

0 ........ goes to the beginning of the line
v ........ visual selection
g_ ....... goes to the end of the line without line break
"ay  ..... copy to the register "a"

If you want to swich modes you can type Esc literally by typing Ctrl-vEsc

You can also use "let" to set a register, in this case a register "a" to swich case of the line and go to the next line:

let @a="V~\<Esc>0j"

The let option allows you to save macros in your ~/.vimrc, wich comes in handy for those situations where you need the macro in a daily basis.

Another advantage of using "let" to assign and reassign your macro is that you can insert special keys this way:

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

Is necessary to use double quotes otherwise it will not work as expected.

Note: The digraph ^[ is inserted by typing Ctrl-vEsc and it represents the Esc literally.

Zebadiah answered 21/10, 2018 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.