John Zeng
John Zeng

Reputation: 1224

How can I find the position of matching brace or bracket in vim script

I know that with % I can jump to the matching brace and bracket, but what if I just wanna know the position of the matching brace and bracket? Are there any function to get that ? Like

int test(int a){return a}

My cursor is at the end of this line after the }, and so the column is 25, I wanna know its matching brace({)'s position , which should be 16.

Upvotes: 1

Views: 1099

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32966

Either you play with % and then record cursorpos() or line() + col(). Or you can use searchpairpos(), but don't forget to specify a rule to ignore bracket characters in comment context.

e.g.

let s:skip_comments = 'synIDattr(synID(line("."), col("."), 0), "name") =~?'
      \ . '"string\\|comment\\|doxygen"'

function! s:SearchBracket()
  let flag = 'cnzW'
  return searchpairpos('{', '', '}', flag, s:skip_comments)
endfunction

Upvotes: 2

Related Questions