I'm trying to create a function mapping that takes a count, in this case the number of lines to yank to the system clipboard.
func! Clipboard()
echo 'Clipboard function called'
if v:count is 0
normal "+yy
else
exec 'normal ' . v:count . '"+yy'
endif
endfunc
nmap YY :call Clipboard()<CR>
Problem is it's being called multiple times when a count is provided and even then the clipboard ends up with only one entry. Pretty bizarre.
e.g. with this text
first line
second line
third line
fourth line
fifth line
sixth line
seventh line
eighth line
I place the cursor at 'first line' and hit '4YY' and the clipboard ends up with these lines:
fourth line
fifth line
sixth line
seventh line
How's this happening?
The messages printed are:
4 lines yanked
4 lines yanked
4 lines yanked
4 lines yanked
If the function is really being called 4 times like the message says, why do I have only one entry in the clipboard?
:h func-range
, or use:<C-u>call Clipboard()<CR>
in your map.range
, the function handles the range itself (a:firstline
,a:lastline
), so it's called only once. See here. The other approach (using<C-u>
) works too - you clear the range prior to calling your function, so it's called only once, but you still have access tov:count
(alsov:count1
) - so it's probably easier to use in your case.noremap YY :y<cr>
, with visual mode support.