How to search for line containing unclosed parenthesis.


works only for non-nested parenthesis.

If the closing parenthesis is present in the next lines, it should still be a match.

for example :

foo( bar, bar
     bar, bar );

should be a match.


It is hard to match balanced parentheses with regular expressions, but fortunately, Vim has built-in motions for ( and {. See :help [( and :help [{.

| improve this answer | |
  • sadly, there is no way to limit the range of [(, as i am looking for unclosed parenthesis in the range of one line, updated my question to make more obvious. – Mathieu Westphal May 15 '15 at 13:51

Done it myself

function Unclosed(pattern, pattern2)

    " Store current pos
    let l:matchpos = getpos('.')

    " Initialize return
    let l:ret = 0 

    " Search Pattern, no wrap
    while search(a:pattern, "W", "", "") > 0 

        " Search for pair only in current line
        if eval(searchpair(a:pattern, '', a:pattern2, '', '', line(".")))

        " found store position and line
        let l:matchpos = getpos('.')
        let l:ret = line(".")

    " Set cursor to position and return
    call setpos('.', l:matchpos)
    return l:ret
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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