select the word under the cursor

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

select the word under the cursor

Neshama Parhoti
Hi all,

Is it possible to (visually) select a word under the cursor ?

The reason I need that is to easily copy and paste words under the
cursor,
without having to manually select them with 'v' first
(which is slow).

I know that '*' and '#' finds me occurrences of the word under the
cursor,
so I guess VIM already have the underlying brain to also select the
word.

thank you
pnesh

--
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: select the word under the cursor

Andrey Voropaev
On Thu, Mar 11, 2010 at 9:24 AM, Neshama <[hidden email]> wrote:
> Is it possible to (visually) select a word under the cursor ?

byw or bvw

the first one yanks the word, you can paste it later using p, the
second visually selects the word (why would you need it?)
If you don't want to hit 3 buttons, create a macro. If the "word" you
are talking about is anything from space to space, then instead of w
specify exact character that you want. See :h left-right-motions

HTH
Andrei

--
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: select the word under the cursor

JohnBeckett
In reply to this post by Neshama Parhoti
Neshama wrote:
> Is it possible to (visually) select a word under the cursor ?
>
> The reason I need that is to easily copy and paste words
> under the cursor, without having to manually select them with
> 'v' first (which is slow).

It is very easy to type the following (in normal mode):

  yiw        (yank inner word)
  viw        (visually select inner word)

The first copies the current word.
The second just selects it (as per your first line).

If you want to make it really easy because you are doing a lot
of copying, you could enter some mappings (pick one of these):

  :map <F1> :let @@=expand('<cword>')<CR>
  :map <F2> :let @+=expand('<cword>')<CR>
  :map <F3> yiw
  :map <F4> "+yiw

The F1 and F2 examples use the same logic as * does to search
for the current word. F1 copies to default register, F2 to the
clipboard.

The F3 and F4 examples use the slightly different logic of
"inner word".

John

--
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: select the word under the cursor

Christian Brabandt
In reply to this post by Neshama Parhoti
On Thu, March 11, 2010 9:24 am, Neshama wrote:
> Is it possible to (visually) select a word under the cursor ?
>
> The reason I need that is to easily copy and paste words under the
> cursor, without having to manually select them with 'v' first (which
> is slow).
>
> I know that '*' and '#' finds me occurrences of the word under the
> cursor, so I guess VIM already have the underlying brain to also
> select the word.

If you do
:set guioptions+=a
vim will automatically copy the visual selection into the clipboard. Now
with a little mapping, vim will copy your search phrase into the
clipboard:
:nnoremap * g*:let @+=@/<cr><c-l>

(You would probably also want to map #). The drawback is, that this uses
g* to search which means the search won't consider word boundaries (*
searches for the word under the cursor, while g* searches for the
pattern under the cursor and thus also finds submatches, e.g. if you
press * on a, this will only find the word a, while g* on a also matches
ab). To fix this, you could may be use this mapping:

:nnoremap * *:let @+=@/[2:-3]<cr><c-l>

(which removes the word-boundary strings from the search string and puts
the result in your clipboard.)

A different approach would be to map */# to have the word selected
automatically. Something like this:
:nnoremap * viw<esc>*

This has the disadvantage to move your cursor to the first letter in the
selected word, which may be undesired.

All this only works with gvim, though.

regards,
Christian
--
:wq

--
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