ryskajakub
ryskajakub

Reputation: 6431

don't stop mapping on not found

There is a important part of vim mapping:

:map <C-j>r f{^

I noticed, that when the { character is not found in the line, then the rest of the mapping is not executed.

Is there a way, how to force the mapping to continue even though the search character is not found? (In this case execute the return to the beginning of the file)

Upvotes: 2

Views: 216

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172590

Yes, Vim aborts on an error in a mapping. You can use :silent! normal! ... to continue regardless of errors. With a sequence of :normal! commands, you can even check (e.g. whether the cursor moved) and react to it. Sketch:

:map <C-j>r :exe 'normal! maf{'<Bar>if getpos("'a") == getpos(".")<Bar>echo "no move"<Bar>endif<CR>

Note that this doesn't scale well. You're better of moving the commands into a :function.

Also, you should probably use :noremap; it makes the mapping immune to remapping and recursion.

The literal answer to this question would be: :map <C-j>r :silent! exe "normal f{^"<CR>

Upvotes: 2

Related Questions