pritesh_ugrankar
pritesh_ugrankar

Reputation: 171

How to ignore Characters while matching in vim

I'm trying Vim (gvim on windows 7) since past few days and must say its very interesting though it does have a steep learning curve.

I have a decently built _vimrc file now, all thanks to this forum, but am looking for a specific pattern ignore technique.

Suppose, in a text file I have a string like so:

10000000c9abcdef

and in the same file I have the same string, just separated with : at every 2 characters like so,

10:00:00:00:c9:ab:cd:ef

Or, I have a line such as,

hostname portname switchname

as well as

hostname_portname_switchname

how can I make vim match 10000000c9abcdef with 10:00:00:00:c9:ab:cd:ef and match hostname portname switchname with hostname_portname_switchname ?

As of now, if I place the cursor below 10:00:00:00:c9:ab:cd:ef and hit *, it just highlights and matches the 10:00:00:00:c9:ab:cd:ef, but how can I make it match 10000000c9abcdef ? or hostname_portname_switchname with hostname portname switchname ?

Basically, I want vim to ignore the Non Word Characters ( assuming that non word characters would be a space, tab, :, ::, comma, colon, semicolon etc.)

Is there a way ?

Upvotes: 2

Views: 628

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172768

After selecting the match with *, you can modify the search pattern to change any literal non-keyword character (\k\@!.) to match any non-keyword, optionally (\?):

:let @/ = substitute(@/, '\k\@!.', '\\%(\\k\\@!.\\)\\?', 'g')

To also make this work with _, you have to either adapt the regular expression, or :set iskeyword-=_.

Upvotes: 1

Amadan
Amadan

Reputation: 198546

Not too easily. You would define a function that would extract the word under cursor, remove any non-alphanumerics, insert [^a-z0-9]* between each character, then search on that; and then map # and / to call that function. It would however also match hos - tnamep; ort - names - witch - name.

Upvotes: 0

Related Questions