I tried to figure if Vim had some option for this and I believe it doesn't. So I wrote a Vim script to do the job. Just add it to your .vimrc
.
function Percent_nextline()
let line = getline('.')
if line =~ "{"
normal! %
else
let line = getline(line('.')+1)
if line =~ "{"
normal! j%
endif
endif
endfunction
nnoremap g% :<C-u>call Percent_nextline()<cr>
onoremap g% :<C-u>execute 'normal! V'<Bar>call Percent_nextline()<cr>
This script maps the behaviour you want to g%
. With the cursor on the line class A
you can type g%
and the cursor will jump to the closing curly bracket. It can be preceded with commands like c
, d
and y
.
I implemented this by simply checking if the current line contains a {
. If it does then the command behaves as %
. If it doesn't the following line is checked and if a {
is found the %
command is called upon that line. The onoremap
makes it possible to use the mapping in operator-pending mode, which is used by c
, d
and y
.
I am sure this script could be improved. It is my first :)
I want to thank @IngoKarkat for helping me in How to make a movement function work with delete, yank, etc.