Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Macros: Insert Mode

Tags:

vim

I am trying to create an insert macro where if I type in the command @f it should print out a for loop. I got that part working. However, I would like to leave the user in insert mode before the 1st semicolon. I tried to leave the last command before I quit the record as 'a' for append, but that did not work. Any suggestions?

like image 871
Ria Avatar asked Apr 26 '14 17:04

Ria


People also ask

How do I start vim in insert mode?

To go into INSERT mode from COMMAND mode, you type i . To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode.

How do I exit insert mode in Vim?

Type i to switch into insert mode so that you can start editing the file. Enter or modify the text with your file. Once you're done, press the escape key Esc to get out of insert mode and back to command mode. Type :wq to save and exit your file.

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

You can use <C-o>q to finish recording while in insert mode. <C-o> in insert mode allows you to execute one command in normal mode, and then returns to insert mode (see :help i^O).

like image 140
Nikita Kouevda Avatar answered Sep 20 '22 23:09

Nikita Kouevda


I think the answer is to use abbreviations. For example: iabbrev @f for(; ; )<C-O>4<Left> Then in insert mode type @f and when you hit Space after it, the abrevation will be executed and you will got a for( *; ; ), where the * signs the cursor.

like image 34
bimlas Avatar answered Sep 18 '22 23:09

bimlas