My autocomplete for vim inserts something like this for C++ code:
if( ){
}
Inside the ()
is where my cursor is going to be. Suppose I want to type
if(X[i] == 0){
}
I would type X
, then [
and the closing brace will automatically be inserted, ]
.
I then type i
, now my cursor is between i
and closing ]
. How do I get out so that I can keep typing the rest, the == 0
part?
If there is no autocomplete for the if statement, then it would be easy because I do not have to worry about the ( )
. I can just type, if
, (
, X[i]
. To get out of the bracket, I would just use append, A
and then go on to type the rest: == 0 )
.
Normally, I would use the arrow key or hl to move out of the brackets. Is there another way to do this?
There are several parts to your problem, and plugins that solve them -- of course you could reimplement them...
Regarding the auto-pairing, most (all?) bracketing plugins provide a jump over the closing bracket character by hitting the related key. Here for instance you could simply type ]
instead of <right>
or <esc>la
as you would have done without the auto-pairing feature.
Note: contrary to most solutions you could copy paste from a 1990's vi trick, the plugins will permit to redo (:h .
) an insertion where brackets appear.
I'm quite sure the exact technical solution has already been given here and/or on vi.SE.
There is another issue you haven't mentioned: jumping to in-between the curly brackets corresponding to the true branch of the if
. To achieve this, we rely on placeholders. They could be inserted by snippet plugins, or through very advanced abbreviations.
The trendy snippet plugins have discrete placeholders. The plugins I'm maintaining have more intrusive placeholders (lh-cpp, mu-template. Placeholders that my bracketing plugin also inserts by default. What does this means? It means that typing if
or [
will insert the same placeholders that also serve as jump points. As a consequence, I can use the same jump keybinding (CTRL+J by default in terminal vim) to jump after the closing ]
or in between the {}
-- hitting ]
works as well when the cursor is just before a ]
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With