Inside a function I use to initialise some TeX related settings I have the following mapping defined:
vmap <buffer> ucm :s/^\% //<CR>:nohlsearch<CR>
I expected it to allow me to easily uncomment visually selected lines. The analogous:
vmap <buffer> cm :s/^/\% /<CR>:nohlsearch<CR>
does a pretty nice job in commenting. Also analogous mappings for other languages, which use a #, and not a % work just fine. Those last ones look like this:
vmap <buffer> cm :s/^/# /<CR>:nohlsearch<CR>
vmap <buffer> ucm :s/^# //<CR>:nohlsearch<CR>
A sequence of V10jcmV10kucm is supposed to leave the code intact.
So now: What am I doing wrong?
You are adding unecessary stuff.
and
should work for commenting and uncommenting respectively.
The third
/is used to add options such as/cfor “confirm” or/gfor “global”. If you don’t use those options you don’t need this/at all.In your “uncomment” substitution you are escaping
%but%in itself has no special meaning for Vim’s regex flavour. Not only Vim is certainly not going to match it if it’s escaped but\%<something>is used for a bunch of atoms like\%d. So your pattern fails because Vim stumbles upon your\%expecting the rest of the atom and getting “nothing”.