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

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

1 Answer 1

4

For the w, motion, see :h word:

                                                        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.
                                                        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)

So:

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

Your Answer

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

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