How to copy the name of the variable / function under the cursor?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

How to copy the name of the variable / function under the cursor?

Dotan Cohen
Is there a quick way to copy the name of the variable / function under
the cursor (respecting word boundaries) to the clipboard without
having to meticulously highlight and copy it in the regular fashion?

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Jürgen Krämer-4

Hi,

Dotan Cohen wrote:
> Is there a quick way to copy the name of the variable / function under
> the cursor (respecting word boundaries) to the clipboard without
> having to meticulously highlight and copy it in the regular fashion?

  "+yiw

Note that this command will move the cursor to the start of the word.
If you don't want this, you can surround the command with commands to
save and restore the cursor position:

  mp"+yiw`p

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Benjamin Fritz


On Sep 9, 6:12 am, Jürgen Krämer <[hidden email]> wrote:
> Hi,
>
> Dotan Cohen wrote:
> > Is there a quick way to copy the name of the variable / function under
> > the cursor (respecting word boundaries) to the clipboard without
> > having to meticulously highlight and copy it in the regular fashion?
>
>   "+yiw
>

Yes, this will do exactly what you asked. It doesn't tell you much,
though.

For the details, you should know that "iw" is a text object. Text
objects are a very powerful feature of Vim, which you can read about
at :help text-objects. You can used them after operators like d, y, c,
gU, and the like, or in visual mode.

The other part of the answer, "+y, is selecting the clipboard register
with "+ and then starting a yank operation. See :help registers for
details.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

caruso_g
>   "+yiw

Why not directly yiw ?
It will copy the word (or other text object) to the
    "0
buffer which is the latest yanked text and is not affected by
deletions (which are assigned to buffers from 1 to 9).
As stated, to take a look at your register, just digit
    :reg<CR>
To select a register to paste just digit
    "np
where n is the buffer number to select.
Tip: if you don't want to take a look at register, just digit
    "1p
and if is not what you are looking for, just press
    u
to undo and
   .
to repeat the latest command and (drum roll) Vim will automatically
increment the number of the buffer to paste!
Repeat until you find what you were looking for. :)

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Ven Tadipatri
On Sat, Sep 11, 2010 at 3:37 AM, caruso_g <[hidden email]> wrote:
>>   "+yiw

I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
put you into insert mode and then there would be a 'w' character
that's pasted.
Whever I want to paste, I just use 'P'.
   Thanks for the tip on the buffers - it looks quite useful.

Thanks,
Ven

>
> Why not directly yiw ?
> It will copy the word (or other text object) to the
>    "0
> buffer which is the latest yanked text and is not affected by
> deletions (which are assigned to buffers from 1 to 9).
> As stated, to take a look at your register, just digit
>    :reg<CR>
> To select a register to paste just digit
>    "np
> where n is the buffer number to select.
> Tip: if you don't want to take a look at register, just digit
>    "1p
> and if is not what you are looking for, just press
>    u
> to undo and
>   .
> to repeat the latest command and (drum roll) Vim will automatically
> increment the number of the buffer to paste!
> Repeat until you find what you were looking for. :)
>
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Tim Chase
On 09/11/10 12:15, Ven Tadipatri wrote:
>>>    "+yiw
>
> I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
> put you into insert mode and then there would be a 'w' character
> that's pasted.
> Whever I want to paste, I just use 'P'.

"iw" in this context is a "text object" ("inner word") and can be
used like a motion.  Just as you'd use

   y<motion>

to yank over whatever characters were covered by <motion> (such
as "42G" to yank from the current line to line 42, or "$" to yank
to the end of the line), you can use a text object.  In this
case, the "<motion>" is the "iw" text object.

They're a phenomenal feature of Vim and I use them regularly.
You'll want to read up at

   :help text-objects

for all the good stuff, but Vim knows how to operate on a number
of different things like sentences, "stuff in paired delimiters"
(such as parens, square & curly brackets, single & double
quotation marks, paragraphs, etc).

Hope this not only helps you understand what it's doing, but
opens the door to more efficient editing. :)

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Nikolay Aleksandrovich Pavlov
In reply to this post by Ven Tadipatri
Ответ на сообщение «Re: How to copy the name of the variable / function under
the cursor?»,
присланное в 21:15:24 11 сентября 2010, Суббота,
отправитель Ven Tadipatri:

> I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
> put you into insert mode and then there would be a 'w' character
> that's pasted.
No, if vim expects next atom to be a motion. Normal mode commands after which
vim expects motion (this commands are called operators) are listed in |operator|
section of vim help, yank command (y) is one of them.

Текст сообщения:

> On Sat, Sep 11, 2010 at 3:37 AM, caruso_g <[hidden email]> wrote:
> >>   "+yiw
>
> I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
> put you into insert mode and then there would be a 'w' character
> that's pasted.
> Whever I want to paste, I just use 'P'.
>    Thanks for the tip on the buffers - it looks quite useful.
>
> Thanks,
> Ven
>
> > Why not directly yiw ?
> > It will copy the word (or other text object) to the
> >    "0
> > buffer which is the latest yanked text and is not affected by
> > deletions (which are assigned to buffers from 1 to 9).
> > As stated, to take a look at your register, just digit
> >    :reg<CR>
> > To select a register to paste just digit
> >    "np
> > where n is the buffer number to select.
> > Tip: if you don't want to take a look at register, just digit
> >    "1p
> > and if is not what you are looking for, just press
> >    u
> > to undo and
> >   .
> > to repeat the latest command and (drum roll) Vim will automatically
> > increment the number of the buffer to paste!
> > Repeat until you find what you were looking for. :)
> >
> > --
> > You received this message from the "vim_use" maillist.
> > Do not top-post! Type your reply below the text you are replying to.
> > For more information, visit http://www.vim.org/maillist.php

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

caruso_g
In reply to this post by Ven Tadipatri
> >>   "+yiw
>
> I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
> put you into insert mode and then there would be a 'w' character
> that's pasted.

if i is the first character you digit, yes, it goes in insert mode,
while, using it before a text-object, it means "inner"-text-object, in
this case, "inner word".
So, reassuming, the complete command is:

    yiw

Which means:
    y -> yank
    iw -> inner word on which the mouse is on regardless the cursor's
position on the word

to paste it, just press p or P

Note, to paste a text from a buffer (:reg to see them) you must first
select the buffer name and then press p.
E.g. to paste the buffer number 7, you must digit "7p

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Christian Brabandt
Hi caruso_g!

On Sa, 11 Sep 2010, caruso_g wrote:

> Note, to paste a text from a buffer (:reg to see them) you must first
> select the buffer name and then press p.
> E.g. to paste the buffer number 7, you must digit "7p

Buffers are files that are edited within vim.
You are talking about registers.

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Jürgen Krämer-4
In reply to this post by Ven Tadipatri

Hi,

Ven Tadipatri wrote:
> On Sat, Sep 11, 2010 at 3:37 AM, caruso_g <[hidden email]> wrote:
>>>   "+yiw
>
> I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
> put you into insert mode and then there would be a 'w' character
> that's pasted.
> Whever I want to paste, I just use 'P'.
>    Thanks for the tip on the buffers - it looks quite useful.

you should always try to read Vim commands from left to right; starting
in the middle will often lead you astray. So let's check letter for
letter:

  " is used to address a register. The next letter is used as its name.
    Lower-case letters are used to address "normal" registers. With
    upper-case letters you can append to the corresponding lower-case
    named registers. Numbered registers are used to store the result of
    yank and delete commands. And some registers named by symbols are
    used for special purposes.

  + is the name of the register, in this case the register that is
    connected to the clipboard.

  y is the letter for the yank command. Unless used you are in visual
    mode it must be followed by a movement or a text-object.

  i is not a movement per se, but the start of a text-object. Together
    with the next letter it will select part of the text without the
    surrounding white space or, if you are already on white space, it
    will select the white space.

  w completes the text-object. Together with the i before it selects the
    word under the cursor, which as a result of the whole command gets
    copied to the clipboard.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to copy the name of the variable / function under the cursor?

Dotan Cohen
On Mon, Sep 13, 2010 at 08:27, Jürgen Krämer <[hidden email]> wrote:

>
> Hi,
>
> Ven Tadipatri wrote:
>> On Sat, Sep 11, 2010 at 3:37 AM, caruso_g <[hidden email]> wrote:
>>>>   "+yiw
>>
>> I'm sorry - I'm not understanding what you're doing here. Wouldn't 'i'
>> put you into insert mode and then there would be a 'w' character
>> that's pasted.
>> Whever I want to paste, I just use 'P'.
>>    Thanks for the tip on the buffers - it looks quite useful.
>
> you should always try to read Vim commands from left to right; starting
> in the middle will often lead you astray. So let's check letter for
> letter:
>
>  " is used to address a register. The next letter is used as its name.
>    Lower-case letters are used to address "normal" registers. With
>    upper-case letters you can append to the corresponding lower-case
>    named registers. Numbered registers are used to store the result of
>    yank and delete commands. And some registers named by symbols are
>    used for special purposes.
>
>  + is the name of the register, in this case the register that is
>    connected to the clipboard.
>
>  y is the letter for the yank command. Unless used you are in visual
>    mode it must be followed by a movement or a text-object.
>
>  i is not a movement per se, but the start of a text-object. Together
>    with the next letter it will select part of the text without the
>    surrounding white space or, if you are already on white space, it
>    will select the white space.
>
>  w completes the text-object. Together with the i before it selects the
>    word under the cursor, which as a result of the whole command gets
>    copied to the clipboard.
>
> Regards,
> Jürgen
>

Thank you Jürgen! I mapped mp"+yiw`p to a function key and it is a
tremendous time saver. Your explanation was invaluable, I make a point
of understanding what I'm doing (both in the editor, and in the
code!), and not "just doing". Thanks!

Have a great week!

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php