Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to copy the word under the caret in Vim

Tags:

vim

vi

copy-paste

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?

like image 988
Dan Walmsley Avatar asked Sep 12 '25 05:09

Dan Walmsley


1 Answers

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.

like image 99
Jason Down Avatar answered Sep 15 '25 23:09

Jason Down