Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using macro in vim insert mode

Tags:

vim

macros

I'm a new user of vim and found macros very useful, but I wonder if there is a way I could use them in insert mode instead of switching to regular mode all the time. It means I want to start and finish recording macros in insert mode.

like image 205
Yar Avatar asked Oct 30 '25 22:10

Yar


2 Answers

As the command to trigger a macro, @, is a normal mode command you are out of luck. You could of course use something like <c-o> in insert mode to execute 1 normal mode command then return back to insert mode. However what you want are wanting to do sounds like it goes against the "vim way".

The vim way means you spend most of your time in normal mode and only go into insert mode for short bursts. Mastering normal mode is where vim gets much of its power. Normal mode allows you to compose operators and motions to operate on chunks of text with little thought, makes simple repeats/redos with the . command possible, and allows for pleasant chunky undo history.

like image 120
Peter Rincker Avatar answered Nov 02 '25 04:11

Peter Rincker


You can use Control-O to go out of insert mode, do one macro command and then go back to insert mode.

In practice it is 5 key presses: Control Shift O @ macro_key

But can use inoremap in your .vimrc file to remap a key to multiple key presses!

On my keyboard, the ù key is never used in practice and my .vimrc file contain the lines:

inoremap ù <C-O>@

Now I can execute a macro in insert mode by pressing the ù key followed by the macro key.

like image 38
leszek.hanusz Avatar answered Nov 02 '25 04:11

leszek.hanusz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!