2

What is best way to move cursor from the inside surrounds '"([{ (and other types)?

For example, I'm typing if (condition) { body }. I've typed the condition, then I should go to body.

For me it's very annoying to esc, move cursor, go to insert mode

6
  • One way is to use a snippet system (Ultisnips, Neosnippet <- plugins) and use/define an if snippet that has placeholders for condition and body, placeholders that can be edited and jumped from one to the another. Other than that, in Vim there are many ways to quickly "move cursor" with precision to a desired location.
    – VanLaser
    Commented Sep 7, 2016 at 22:44
  • It's not about snippet system, just go out of brackets and continue typing.
    – megas
    Commented Sep 7, 2016 at 23:16
  • That where snippets and abbreviations with placeholders come in handy. You just hit tab, CTRL-J or whatever the plugin/placeholder system uses and you'll be doing all those tasks at once: <esc>/{<cr>o. The downside is that you need abbreviations or snippets for all the control statements you could use. Fortunately, C control statements are well spread. I even have a for-range loop for C++ in lh-cpp. If some part of the body has already been typed, well, you'll need to define a mapping to do what you wish. Commented Sep 7, 2016 at 23:49
  • 1
    If you find it annoying maybe you don't move your cursor efficiently: how do you move it? Here after typing condition you can do <Esc>f{a which isn't that bad... Now if you really want to cut some keystrokes the plugin solution seems pretty good.
    – statox
    Commented Sep 8, 2016 at 7:32
  • 1
    I would use <C-o> which let you execute a normal command before returning in insert mode: <C-o>fb
    – nobe4
    Commented Sep 8, 2016 at 9:25

1 Answer 1

3

You could start from this map (and improve it according to your taste):

inoremap <C-x><C-x> <Esc>/[[({,]\_s*/e<CR>a

.. which maps Ctrl-x Ctrl-x in insert mode to jump after a set of characters (open brackets, I added comma since it could be useful also to jump over function arguments), skipping also any white spaces or newlines that follow. So, if the text is written, just hit Ctrl-x Ctrl-x to move to the 'next' position, until the cursor is where you want it. How you then change the code that follows is your decision.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.