I am using this excellent solution by Peter Rinker to highlight all occurrences of a word on double clicking.

nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>

Is there a way to modify this to also copy this word into the clipboard, i.e. the "+ register?


The relevant part of that solution is

:let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>

The expand('<cword>') part will grab the text under the cursor, and the escape function will make sure that it's literal text rather than a regex. However, we don't want to modify the text we copy, so we can just call the expand('<cword>') part. So you want to add

:let @+=expand('<cword>')<cr>

to some part of the mapping. Since the order on those three ex calls don't really matter, you can put it wherever you'd like. One solution (adding it to the end) would give:

nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:let @+=expand('<cword>')<cr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.