2022-03-30

Is there a way for Vim to define keywords defined by regular expressions?

I would like to have Vim treat as keywords strings that match:

( "(" <scope> ")" [:_/] ){0,1} <name>

where:

<scope> = [@+-.] ( ":" <name> )*
<name>  = [a-zA-Z][a-zA-Z0-9_]*

A regex to describe this definition of a keyword might be something like:

\(([@_-.]\(:[a-zA-Z][a-zA-Z0-9_]*\)*)\){0,1}[a-zA-Z][a-zA-Z0-9_]*

Some concrete examples:

(@:a):f
(+:b:c)_v
(-)/p
n

Using Vim's set iskeyword does not appear to be able to handle regular expressions.

I've considered expressions such as:

let @/ =~ '\<'.expand('<cword>').'\>

but <cword> doesn't seem up to this task as well.

The use case for this is keying in * to search for words matching the regex that are under the cursor.

I've also considered whether \%# might be part of the solution; however, that seems to require splitting the regex into pre- and post-parts.

Is there a way for Vim to define keywords with regular expressions for use in initiating searches under the cursor, then using n and p to find next and previous matches? Solutions do not necessarily need to involve iskeyword. I'm suspecting that using @/ is part of the solution. Your thoughts?



No comments:

Post a Comment