简体   繁体   English

在vim中自动编写循环?

[英]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. 而且我知道有办法在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? 如果X和Y可以改变,一旦我以某种方式提供X和Y,我可以在vim中做一些东西来写一个for循环吗?

You can use a plugin like snipMate to store snippets, which you can then store a for snippet in. 您可以使用插件像snipMate存储片断,然后你就可以存储for摘录。

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 : 这是Vim wiki缩写的一个例子:

: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 . 请注意,所有^X字符都是通过Ctrl V Ctrl X输入的。

You can then run this macro with @f . 然后,您可以使用@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. 编辑:刚看到你问题的第二部分(不确定你是否编辑过它?也许我读错了。)但是如果X和Y要改变,我会建议一个更有特色的解决方案,比如snipMate.vim。

The most evolved way is indeed to use an snippets engine. 最进化的方式确实是使用片段引擎。 snipMate has been evoked. snipMate已被唤起。

I'm maintaining mu-template that is the root of the advanced snippets from lh-cpp . 我正在维护mu-template ,它是lh-cpp高级代码片段的根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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