Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append keys to an earlier recording in vim

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?

like image 812
Felix Avatar asked Oct 21 '18 11:10

Felix


People also ask

What is recording @W in Vim?

Recording is a really useful feature of Vim. It records everything you type. You can then replay it simply by typing @ <letter> . Record search, movement, replacement...

How do you repeat a macro in Vim?

To replay the macro once, move the cursor to the next line and press @h where h represents the register on which you saved the macro. Notice that the macro automatically moves the cursor to the next line as required. This allows you to repeat its execution. To repeat the macro execution, press @@.

Do Vim macros persist?

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.


2 Answers

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.

like image 200
Marth Avatar answered Oct 18 '22 22:10

Marth


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.

like image 3
SergioAraujo Avatar answered Oct 18 '22 23:10

SergioAraujo