vim - how do I yank to register inside a function?

Clash Royale CLAN TAG#URR8PPP
vim - how do I yank to register inside a function?
I am perplexed how to use the registers from inside a function. For example, if I want to yank the current word to register "k", from inside vim, I would use the command/keystrokes
"kyw
But this does not work from inside a function: " starts a comment:
"
function MyFunction()
"kyw
"^^^ does not work because it is a comment...
let @k="I can set register k directly to text..."
" but that's not the same
yank k "I can yank an entire line, but still not the same
endfunction
Is there a way to do this without changing the comment option (would that even work?) Thanks; I have been hitting my head against this for an entire day.
1 Answer
1
Use either
:normal! "kyw
:let @k = expand('<cword>')
As it turns out, using variables was easy. Thanks for answering this tho - I was sure there was a way to do it, but couldn't find anything!
– Peter Kay
Jan 6 at 18:53
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you! I have already started looking into whether I can accomplish the same goals with variables; I will perhaps see if it's possible to do it that way and prefer to do it.
– Peter Kay
Jan 6 at 5:15