This question already has an answer here:

I am trying to get the mouse selected text in a register in normal mode. If the selection is a substring, only the selection should be copied, not the complete word.

Currently the script has:

normal! "zyiw

I would use the script on remote servers without X too. vim is compiled without clipboard.

Any help is much appreciated.

marked as duplicate by Mass, Rich, B Layer, Martin Tournoij Apr 7 '18 at 16:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    the mouse selection is in the * register (assuming your vim is built with the right support). what else would you want? – Mass Mar 30 '18 at 17:27
  • I am trying: ``` @z = @* but z doesn't have the text. How can I confirm if my vim has the support for this? – Arun Prakash Jana Mar 30 '18 at 17:31
  • Which feature should I look for in my :version output? – Arun Prakash Jana Mar 30 '18 at 17:35
  • I believe it's the feature +clipboard. – Mass Mar 30 '18 at 17:41
  • I have the default version on Ubuntu without clipboard (-clipboard). Is there any other way to do this? I am trying a modify a script to highlight multiple mouse selected text (even substrings) with different colors. Currently it matches the complete word under cursor. – Arun Prakash Jana Mar 30 '18 at 17:43

Here's what I did finally:

In vim I have:

:map <C-a> :normal! gv"zy<CR>

And in the script I added:

:if @z == ""                                                                                                                            
    normal! "zyiw                                                                                                                       
// do something
:let @z = ""

In general, a visual selection followed by 1. Esc and 2. ^a will be highlighted.

If there is no visual selection, the word under cursor will be highlighted.

The complete script is here.


Selection of text via the mouse (in a plain vanilla vim, no vimrc, no plugins, etc) places the selection in the "* register.

A subsequent assignment,

:let @f = @*

will place the text into the f register. A mapping such as

:map ^va :let @f = @*^v^M

will copy the "* register to the *f register using ^a (control-a). additional configuration will permit using <C-a> and <CR> in place of ^va and ^vM.

Perhaps the simplest way, however, is to select the text with the mouse or one of the "V" commands (V, v, CTL-v) and then enter in normal mode


This will "yank" the current selection into the "Y register.

  • That requires vim to be compiled with +clipboard. – Arun Prakash Jana Mar 30 '18 at 20:07
  • If there is not text selected, I am trying to get the current word under the cursor. – Arun Prakash Jana Mar 30 '18 at 21:06
  • In .vimrc I have - :map ^va :let @f = @*^v^M I have the following in the plugin: :if @f == "" normal! "zyiw :else :let @z = @f :let @f = "" :endif but it's always the word under cursor that I get in z. – Arun Prakash Jana Mar 30 '18 at 21:10
  • Is there a way to clear the visual buffer? The I can select in visual mode, copy to z and clear the visual buffer again (as well as z after use). – Arun Prakash Jana Mar 30 '18 at 21:13
  • you can use the :let @<buffer name> = syntax to set it to anything you want. just curious - why would you want to clear it? – wmmso Mar 31 '18 at 13:13

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