9

Consider a file with the following two lines:

someLongFunction
someFunction

When I am in the insert mode in the middle of the second word

some|Function

and I press Ctrl-n, I get

someLongFunctionFunction

Is there a way of configuring Vim so that I get the following "smart" completion instead?

someLongFunction
5

The following is only a quick mock-up answer (i.e. functional until it doesn't, or until it breaks something :) ), but it shows that "it is possible":

augroup completion
    autocmd!
    autocmd CompleteDone * call PostCompletion()
augroup END

function! PostCompletion()
    if !empty(v:completed_item)
        "check if text after current cursor position is part of the match
        let crt_word = expand('<cWORD>')
        let compl_word = v:completed_item['word']
        let lcw = len(compl_word)
        let leftover = strpart(crt_word, lcw)
        let lfl = len(leftover)
        if lfl > 0
            let endcompl = strpart(compl_word, lcw - lfl)
            if leftover ==# endcompl
                let cpos = getcurpos()
                normal dW
                call setpos('.', cpos)
            endif
        endif
    endif
endfunction

What the above code tries to do is: after completion, verifies if the WORD under cursor is longer than the completed word, and, if so, it further checks if its 'remainder' matches the last part of the completion (in your example, "Function"). If it does, the rest of the WORD is deleted (this assumes some things about cursor position).

(I'm pretty sure there are more clever ways to accomplish all this, and I'd like to see them!)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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