2

I would like to search the word under my cursor. For this, I tried to press *. But, if the word I want to search is #foo, as an example, vim only searches word foo (without hash), so it fails.

In fact, the # is not considered in the word, which is confirmed when I try to go to the next word with w.

Do you know how I can search effectively #titi by pressing * on my keyboard?

2
  • Besides warnings about changing iskeyword consider this: how should all of the non-keyword characters that are fully or partially pattern atoms (i.e. they are special in a regex) be handled. Existing * functionality doesn't need to worry about how to handle *, ?, (, etc. That functionality would need to be well defined.
    – B Layer
    May 5, 2019 at 23:58
  • for the rare cases I need this, I usually just use g* instead of * and continue jumping using n until I find what I am looking for. May 6, 2019 at 11:15

3 Answers 3

1

What is considered to be part of a word is defined, I think, by iskeyword. You can try to change this setting to include # and see if it solves your case.

More info: :help 'iskeyword'

2
  • 1
    Do be careful with this, as it does affect a lot of things
    – D. Ben Knoble
    May 5, 2019 at 17:30
  • Of course it does affect a lot of things! How can i change keywords just for # and - (i want for exemple #foo-bar to be just one word)? i tried before but i did not succeed. The problem is that i also want to use tags with CTRL + ], so maybe i cannot do set iskeyword="" as far as i understand. Mathieu
    – user22113
    May 5, 2019 at 18:47
1

You can use a visual selection first to highlight all the text you want to search for, then run your search. If #foo is surrounded by whitespace, it's even easier; you could simply do viW*.

Reference: :help v_star-default

Note: This is Neovim specific does not work in Vim

1
0

A solution without changing the iskeyword option or using * would be to put the whole white-space delimited word (see :help WORD) under the cursor with Ctrl-RCtrl-A (see :help c_CTRL-R_CTRL-A) into search, or:

/<C-R><C-A><CR>

Now * (as opposed to g*) also has the nice touch of wrapping the word in boundaries like this \<foo\>. If you want to match #foo but not #foobar, you need to add them yourself:

/\<<C-R><C-A>\><CR>

This is a little more typing than a simple * but not too much (I think) for occasional use.

If you need to search for strings like #foo frequently, I'd suggest creating a mapping like nnoremap <lhs> /\<^R^A\><CR> where <lhs> is the key you want to use and ^R^A is produced by pressing the sequence Ctrl-VCtrl-RCtrl-VCtrl-A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.