Vim script: check if the cursor is on the current word

vim

Solution

An alternative using the position for (next) `<cword>` is: (according to a comment by OP he want to know, perhaps the question should be updated with that request)

let cInd = strridx(getline('.'), expand("<cword>"), col('.') - 1)
let isOnKeyword = (cInd >= 0 && (cInd + strlen(expand("<cword>"))) >= (col('.') - 1))

The second part is needed if the next `<cword>` is also in an earlier position on the current line, in your example if the string `arg2` should have been in the line also somewhere before the `,`, that is hypothetical but should be considered. Note that it's `strridx` not `stridx`.

Problem

For example in this situation: ``` function_call(arg1, arg2); ^Cursor is here ``` calling `expand('<cword>')` will return "arg2". I need to filter out these cases, when cursor is on left from the current word's position. Is there a way to do it?

Original source