简体   繁体   中英

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?

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.

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.

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 Ctrl V Ctrl X .

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM