function! s:foo()
    normal! yiw
    if @" =~# '\vf[0-9]b\('
        echom 'Matches!'

nnoremap <silent> * :<c-u>call <sid>foo()<cr>

" f2b⣿(

When my cursor position is and I press *, why doesn't it say 'Matches!'. I just can't get any braces to match (<, (, [, or {).

I would also be very happy about any recommended references, as I h regex or h =~ isn't really helpful, as it just covers the basics...

  • 2
    Does iskeyword contain these characters (<, (, [, {)? See :h word for what w uses. – muru Jun 19 '16 at 11:03
  • Okay wow that was stupid, of course that's it. – hgiesel Jun 19 '16 at 11:05

For the w, motion, see :h word:

A word consists of a sequence of letters, digits and underscores, or a
sequence of other non-blank characters, separated with white space (spaces,
tabs, <EOL>).  This can be changed with the 'iskeyword' option.  An empty line
is also considered to be a word.
A WORD consists of a sequence of non-blank characters, separated with white
space.  An empty line is also considered to be a WORD.

So, you probably need to set iskeyword, or use W.

However, depending on what your actual use for this thing is, you don't need to copy the text. Use <cword> (or <cWORD>):

    <cword>    is replaced with the word under the cursor (like star)
    <cWORD>    is replaced with the WORD under the cursor (see WORD)


function! s:foo()
    let l:word = expand('<cWORD>')
    if l:word =~# '\vf[0-9]b\('
        echom 'Matches!'
| improve this answer | |
  • I love how you turned a stupid question into an answer where I still learned a lot! – hgiesel Jun 19 '16 at 11:24

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.