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.

  • 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
  • You should clarify whether you mean visual selection via mouse. If so you would just use normal! gv"zy (mouse has no bearing on this, it's just visual selection). – Mass Mar 30 '18 at 19:19
  • I am trying to modify it that way: visual selection - gv"zy and if not "zyiw. Now how do I clear the visual buffer after the text is copied to "z so I can use the technique next time? – Arun Prakash Jana Mar 30 '18 at 20:08

2 Answers2

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.

wmmso
  • 64
  • 3
  • 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 @ = syntax to set it to anything you want. just curious - why would you want to clear it? – wmmso Mar 31 '18 at 13:13
  • If I don't clear it the variable retains the data from previous visual copy. – Arun Prakash Jana Mar 31 '18 at 15:02
  • are you using the real control-v or are you typing v? – wmmso Mar 31 '18 at 19:27
  • is having something in the "* register a problem? what is the overall effect you're trying to accomplish. it really isn't necessary to clear the register as it'll get overwritten the next time you copy something to it. why is having something in the register a problem? – wmmso Mar 31 '18 at 19:36
  • if you have a standard distribution such as ubuntu, you can prob'ly use the notation. :map :let @f = "* you're checking +clipboard using :ver, right? seems like the standard ubuntu full version would be compiled with it (i use a ubuntu derivative - mint). do you have the large full vim installed or the minimal vim? does using the mouse to select actually put stuff into "*? – wmmso Mar 31 '18 at 19:49
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.