In hope of answering Using the % command on a line without a bracket I tried to write my first Vim script.
My thought was to check if there was a {
on the current line. If there was one I wanted %
to behave as usual. If not %
would be called on the next line by doing j%
. This is what I wrote
function Percent_nextline()
let line = getline('.')
if line =~ "{"
normal %
else
normal j%
endif
endfunction
nnoremap g% :call Percent_nextline()<cr>
Then I tried it on this file
foo
{
{ }
}
With the cursor on the first line typing g%
does indeed move the cursor to the last }
. Then I tried dg%
, meaning to delete all the block. This failed, and made the bell sound. The buffer was left unchanged.
So I have two questions:
How could I make commands like
c
,y
,d
, etc. work with this new mapping?If I want to use just
%
rather thang%
how could I stop the function from recursing?