1

Looking for simple and straightforward solution. I'd like to jump out of parenthesis, bracket, brace, quotes etc while typing in "insert" mode. Obviously following mapping settings in ~/.vimrc work only for angle bracket in the last line:

inoremap <C-j> <Esc>f)a
inoremap <C-j> <Esc>f]a
inoremap <C-j> <Esc>f}a
inoremap <C-j> <Esc>f"a
inoremap <C-j> <Esc>f'a
inoremap <C-j> <Esc>f>a

This mapping using regexp doesn't work either. I wonder why:

inoremap <C-j> <Esc>f[)}"'\]>]a

This one works, but gives me too many highlights on every match:

inoremap <C-j> <Esc>/[)}"'\]>]<CR>a

I've see there are people invent a function for this purpose, but I like a single line solution more, if possible. Any suggestions?

3 Answers 3

1

The answer that uses searchpair() as provided by user Mass is great in finding the next closing ),],}, or >. However, as searchpair() requires the opening and closing characters to be distinct, it cannot be used to find a closing ' or " (eg. searchpair('"', '', '"', 'W') will not work).

From the comments, the OP wishes to use search() instead, in order to search for ' and " as well. This can be achieved with

inoremap <C-j> <C-\><C-n>:call search('[>)\]}"'']', 'W')<CR>a

Note that in the regex '[>)\]}"'']',

  • the ] needs to be escaped,
  • the double quote should not be escaped, since it is within single quotes, and thus is treated literally,
  • a literal single quote is given by two single quotes (ie. '').

One can refer to :h expr-' for details (and possibly contrast this with :h expr-")

1
  • Thanks. It helps a lot. Indeed this one works. A bit tricky though.
    – D.W
    Commented Apr 15, 2022 at 1:20
1

There is a function searchpair which goes to the end of the outer matching pattern. In this case, we can use it to match [({. It's also better than f or / since it is actually the enclosing pair, not just whatever happens to be next.

With knowledge of this function, it just remains to call it from insert mode:

inoremap <silent> <c-j> <c-\><c-n>:call searchpair('[([{]', '', '[)\]}]', 'W')<cr>a

A bit of a lengthy one-liner but it is something that could by typed fully on the cmdline, if one wanted.

I chose to use <c-\><c-n> to drop into normal mode followed by a. This will ensure the cursor is placed after the closing ) and not moved if there is no ). Normally, one would try to use <c-\><c-o> to avoid true normal mode, but this would necessitate an additional cursor movement, which in turn could not be canceled if the searchpair fails. Fortunately, situations where this difference would matter are rare.

2
  • Function searchpair() looks good. I still wanna add " and ' to the list. Enlightened by the idea of using built-in function, I tried search() function, inoremap <C-j> <C-\><C-n>:call search('[>)\]}\"\']', 'W')<CR>a. It doesn't work. It would work if I get rid of \"\' from the pattern. What am I doing wrong?
    – D.W
    Commented Apr 11, 2022 at 5:55
  • @D.W The double quote should not be escaped, since it is within '...'. To "escape" the single quote, use ''. From :h expr-': This string is taken as it is. No backslashes are removed or have a special meaning. The only exception is that **two quotes stand for one quote**.
    – husB
    Commented Apr 13, 2022 at 15:22
0

This mapping gives me not perfect but ok result.

inoremap <C-j> <Esc>/[)}"'\]>]<CR>:<C-u>nohlsearch<CR>a

A good thing about it is it can jump to next )}]"' if i keep pressing C-j.

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.