3

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.

2 Answers 2

3

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

1
  • 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. May 15, 2015 at 13:51
2

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(".")))
            continue
        endif

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

    " Set cursor to position and return
    call setpos('.', l:matchpos)
    return l:ret
endfunction
0

Your Answer

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

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