nakiya
nakiya

Reputation: 14413

Automate writing for loop in vim?

I keep writing for loops today. All of them have the format :

for(size_t szI = X; szI < Y; ++szI)
{
   //Something
} 

And I know there are ways to record actions in vim. If X and Y can change, can I do something in vim to write a for loop once I supply X and Y somehow?

Upvotes: 4

Views: 1692

Answers (4)

Luc Hermitte
Luc Hermitte

Reputation: 32926

The most evolved way is indeed to use an snippets engine. snipMate has been evoked.

I'm maintaining mu-template that is the root of the advanced snippets from lh-cpp.

Upvotes: 1

Randy Morris
Randy Morris

Reputation: 40927

I'd use a macro:

let @f = "ifor(size_t szI = X; szI < Y; ++szI)^M{^M}^[O^T"

Note all ^X characters are entered via CtrlVCtrlX.

You can then run this macro with @f.

Edit: Just saw the second part of your question (not sure if you edited it? Maybe I read wrong.) but if X and Y are going to change, I'd suggest a more featured solution such as snipMate.vim.

Upvotes: 1

You can use a plugin like snipMate to store snippets, which you can then store a for snippet in.

In fact, it already comes with a lot of predefined snippets, a lot of which may prove useful.

Upvotes: 7

ulidtko
ulidtko

Reputation: 15560

This is one example from Vim wiki on abbreviations:

:abbreviate forl for (int i = 0; i < ; i++) {<esc>7hi

Not exactly what you wanted, but close to that.

Upvotes: 1

Related Questions