Reputation: 36161
It's really easy to insert a closing brace after typing the opening one:
inoremap { {<CR>}<Esc>ko
This way
if (true) {
converts to
if (true) {
|
}
But I'd like to save time and to type 1 character less:
if (true)<CR>
So I'd like to create the following rule: if return is pressed and the line starts with if/for/while, execute {<CR>}<Esc>ko
Is this doable?
Thanks
Upvotes: 1
Views: 143
Reputation: 196826
Building on your previous mapping, this should do what you want:
inoremap )<CR> ) {<CR>}<Esc>ko
However, you should try a snippet expansion plugin like SnipMate or Ultisnips. Both plugins allow you to define snippets with placeholders and miroring (lots of them are included by default) that are expanded when a <Tab>
is pressed after a trigger.
For example, you have a snippet associated with the trigger if
that expands into:
if ([condition]) {
}
condition
is selected, ready for you to type, and, once you are done, you can hit <Tab>
again to jump the cursor between the curly braces, ready to type:
if (myVar == 5) {
|
}
This is incredibly handy.
Upvotes: 4