8

Use Case

I'm in normal mode. I've not explicitly thought about recording a macro, but I make a single command change. I can now repeat this action by using dot.

I now realize it's a useful action to save in a macro to use later rather than typing the whole command every time. I can either attend to all the changes which need this command right now by using dot, or I can continue with editing and use this command later by saving it in a macro.

The latter is what I want to do, but the easiest way I can think of doing it is not easy - undo my change, start recording a macro and retype the command to make the required change and stop recording the macro.

What I've Tried so Far:

Recording dot in a macro doesn't work because it records the dot and not the change which dot performs. (For example, let's say the command stored in dot was d5w If you start recording a macro and do the action via . expecting that it'll delete 5 words every time you run the macro, then that will not happen. The next time you run the macro it'll do dot and not the command d5w. This means that if you made some other change after recording the macro - say delete 3 words using d3w and it was the last change you performed, then when you run the macro it will delete 3 words and not 5, because it's just running .)

  • I'm struggling to understand both what you want to do, and why you don't do whatever that is with a macro. – user859 Mar 28 '18 at 12:56
  • 1
    @DrEval The OP realises after performing a normal-mode command that they should have saved it in a macro, and doesn't want to have to type it again while recording the macro. – Rich Mar 28 '18 at 13:10
1

Vim doesn't keep a history of your normal mode commands. If you do wish to keep a history of your normal mode history, you can use the following to save that to a file: https://vi.stackexchange.com/a/587/13848

Normal mode commands are usually short, so people just type it out in normal mode and not try to create macros. However, if you do want to create macros out of normal mode commands, you can convert it into a Ex mode command and then save it into a macro as @wmmso pointed out. You can do this by using the norm command. So if you want to delete 2 word starting at the current cursor position, and use a normal mode command in Ex mode use :norm d2w. (Ref: http://vimdoc.sourceforge.net/htmldoc/various.html#:norm, Execute normal command over range)

Its better to use a Ex mode command if it is a complicated command, as it allows you to type out the full command in a buffer, make sure that its actually correct and also saves it in the command line history.

Now that you have your normal mode command in ex mode, you can also browse your command like history using q:, and then copy it into a register using "ay$, while on the line containing the command line you want to save to a macro. Then when you want to run the macro, you can execute it using @a

  • How would you write a normal mode command in the command line though? for instance d2w? – Peeyush Kushwaha Jul 1 '18 at 18:28
  • Added some clarifications in the answer.. Does that help? – alpha_989 Jul 1 '18 at 22:13
0

copy the content of the ":" register (what you just did) into another register, eg., register a:

:let @a = @:

you can execute it again using

@a
  • This doesn't work for me in vim 7.4. Seems that the register : contains the last executed command via :command-name-goes-here instead of a normal mode command such as d2w – Peeyush Kushwaha Apr 16 '18 at 10:00
  • That solution only works for ex commands not normal mode commands. I'm unaware of a register that holds the last normal mode command. Would be useful – Steve May 1 '18 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.