Dan Walmsley
Dan Walmsley

Reputation: 2821

Easy way to copy the word under the caret in Vim

I am trying to find an easy way to copy the word that the caret is currently on top of. I know that I can select to the front of the word: press v e y. But this seems crazy, I can simply press * above a word to search for it, surely there is a better way to copy the word. Maybe even in a single key press?

Upvotes: 4

Views: 905

Answers (1)

Jason Down
Jason Down

Reputation: 22171

You can use y i w (Yank In Word). Though it is just as many keystrokes. If you are at the beginning of the word you can drop the i and use either y w or y e.

Alternatively you can map the command to a key any way you like. For example, you could put this in your vimrc file:

nmap <F8> yiw

The F8 key is right near the * key so it would be easy to remember that it acts similar to the * word highlight. This would be a single key to yank the word.

UPDATE:

Satoru.Logic's comment is definitely a good way to go. If you are not sure what <leader> means, have a look at this post.

Upvotes: 13

Related Questions