How to find (“super-star”) the word under the curs

2019-08-12 08:24发布

问题:

Vim's "super-star" operation (search and highlight for the word under the cursor) is super-handy. But it has the limitation of searching for the exact match of the whole word.

Is it possible to have similar functionality, but w/o the enclosing word-boundary brackets?

Update: Apparently I was too quick to post the question. Looking here, there's the answer.

回答1:

using g * is probably the best solution, but for new vim users it might be interesting to know how this could be done pretty much from scratch:

:nnoremap <expr> * "/".expand("<cword>")."/\<CR>"

map <expr> means that instead of mapping to a literal key sequence, the right-hand side is an expression that is evaluated every time the mapping occurs and the resulting string is treated like the RHS of a normal mapping.

expand("<cword>") returns a string containing the word under the cursor, and "\<CR>" basically just means the enter key.

So in the end we get a mapping to /<word under cursor>/enter, where the word under cursor isn't inserted until the mapping is used.



回答2:

Answer is in this page: Instead of pressing *, use g*.



标签: vim find