3

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.

7
  • 1
    the mouse selection is in the * register (assuming your vim is built with the right support). what else would you want?
    – Mass
    Commented Mar 30, 2018 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? Commented Mar 30, 2018 at 17:31
  • Which feature should I look for in my :version output? Commented Mar 30, 2018 at 17:35
  • I believe it's the feature +clipboard.
    – Mass
    Commented Mar 30, 2018 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. Commented Mar 30, 2018 at 17:43

2 Answers 2

1

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

"gy

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

9
  • That requires vim to be compiled with +clipboard. Commented Mar 30, 2018 at 20:07
  • If there is not text selected, I am trying to get the current word under the cursor. Commented Mar 30, 2018 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. Commented Mar 30, 2018 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). Commented Mar 30, 2018 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
    Commented Mar 31, 2018 at 13:13
0

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                                                                                                                       
:endif
...
// 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.

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