0

As question imply, I'm trying to make a command based on the word under cursor, but that is between two character, in this example, asterisk.

Inspired from here, I made:

map <F3> "zyiw:exe "h "expand("<cword>")""<CR>

Where i use it on h for testing.

*E426* "works
*:ta*  "works
*:tag* "only match *tag* instead of *:tag*
*tag*  "works

But even though it seems to work fine for *:ta*, it doesn't seem to work right for *:tag*.

I'm aware one need to add characters/symbol to iskeyword so that it is correctly matched as a word, but in this instance, i didn't need to do so (though that only worked for *:ta* and not *:tag* as previously mentioned).

Slightly related to this post.

4
  • 1
    I would have written your mapping as nnoremap <F3> :execute 'help' expand('<cword>')<cr> but I’m not sure it makes a difference – D. Ben Knoble Oct 31 '20 at 14:23
  • yeah...any idea to make this work? I tried what was showed in the wikia linked in my post, but that didn't quite worked (which is why i ended up using cword in the first place) @D.BenKnoble – Nordine Lotfi Oct 31 '20 at 19:02
  • 1
    Try using <cWORD> instead. That seemed to work for me. – Tumbler41 Nov 4 '20 at 23:58
  • this...actually work!!! I do not know what is the difference (looked into help but didn't see any difference beside that one is uppercase and the other isn't) but it did took me a longer time than i thought to know this...so thank you (and feel free to post this as answer if you want). @Tumbler41 – Nordine Lotfi Nov 5 '20 at 1:13
2

There's <cword>, which essentially uses the characters in 'iskeyword' to determine what a "word" means, and also <cWORD> which matches a sequence of non-whitespace characters.

For the former, <cword>, by default it will not include symbols such as : and *, so in your specific case, when your cursor is on *:tag* it will only get tag, and when your cursor is on *:ta* it will only get ta.

You seem to be under the impression that it's behaving differently because of how the :help command works when it doesn't match a help topic exactly.

The help system has tags for both the command :tag and an explanation on what a tag is. So :help :tag will take you to the former, while :help tag will take you to the latter.

On the other hand, there's no exact match for ta by itself. So when you look for :help ta, Vim will try to "guess" what you meant by that. In particular, it will try to see if it's a valid Ex command (by prepending a :) and, since there is such a help topic, it will take you to the help on :ta.

If you want to find the exact expression inside the *s, you can either use <cWORD> and then use Vimscript functions to filter out the *s from the resulting expression, or you can use a yank to capture it. For example:

nnoremap <F3> :exe 'help '.substitute(expand('<cWORD>'), '^\*\\|\*$', '', 'g')<CR>
1
  • 1
    perfect. Also illustrated the misunderstanding i had previously :) – Nordine Lotfi Nov 5 '20 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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