I need to create regex in VIM (for plugin purpose) which would work as follows:
Expected behavior:
||
characters indicate current cursor position, bold words indicate matching words
Example nr 1:
- foo bar foo bar
- foo b|a|r foo bar
- foo bar foo bar
Two bar
s from first line match. First bar
from the second line does not match because the cursor is currently placed on it. Second bar
in the second line do match. Two bar
s from third line match.
Example nr 2:
- foo bar foo bar
- foo bar foo bar
- fo|o| bar foo bar
Two foo
s from first line match. Two foo
s from the second line match. First foo
from the third line does no match as the cursor is currently placed on it. Second foo
from the third line do match.
So I was tinkering a bit with it and I managed to create the regex which will match all words like one under the cursor:
" putting word under cursor into variable
let current_word = expand('<cword>')
" regex matching all words like one under cursor
'\k*\<\V'.current_word.'\m\>\k*'
I can not figure out how to exclude word under cursor from matches.
I found this special character \%#
which according to VIM help
Matches with the cursor position. (...)
Yet I couldn't figure out how to use it in my case. Any ideas?