# find all lines containing only a comment char: "#" "," ";"
# and replace with that char and --- ie: #---
:%s/^\(#\|"\|;\)$/\1---/g
The comment says the command should replace these characters:
But the next command replaces these characters:
Which one tells the truth?
After attempting to convert it to a binding (not a let @x persistent macro), it doesn't work:
nnoremap <silent> <leader>tb " :%s/^\(#\|"\|;\)$/\1---/g<CR>
Why is there an orphan double quote between the lhs and rhs of the mapping?
nnoremap <silent> <leader>tb " :%s/^\(#\|"\|;\)$/\1---/g<CR>
^
?
I've read you need to convert the | to I believe it was, but that had zero affect.
This seems to work:
nnoremap <silent> <leader>tb :%s/^\(#\<bar>"\<bar>;\)$/\1---/g<CR>
Tip: after installing a mapping, ask Vim to print how it was installed in the mappings table:
:nno <leader>tb
If the result does not contain the exact sequence of keys you want to be executed, adapt the rhs accordingly.
Is there a vim-plugin or built-in feature that would allow me to convert anything in history to a keybind without manipulation??
Move the rhs of the mapping in a function. The function call will still be subject to the usual mapping parsing, but whatever the function does in its body escape that parsing:
nnoremap <silent> <leader>tb :call Func()<cr>
fu Func()
%s/^\(#\|"\|;\)$/\1---/g
endfu
BTW, this regex:
^\(#\|"\|;\)$
Can be simplified using a bracket expression:
^[#";]$
Also, if you have the patch 8.2.1978, use the pseudo-key <cmd>
to execute an Ex command:
nnoremap <leader>tb <cmd>call Func()<cr>
^---^
It reduces side-effects (no Cmdline*
events, no display on the command-line so no need of <silent>
, ...).
If you can't use <cmd>
, then add <c-u>
to kill a possible range inserted in front of :call
if you hit a count by accident (see :h c^u
):
nnoremap <silent> <leader>tb :<c-u>call Func()<cr>
^---^