« Back to Index

Vim: word motion to be camelcase sensitive

View original Gist on GitHub

Tags: #vim #viml

word-motion-to-be-camelcase-sensitive.viml

function! Word()
    " Get cursor current position
    let curpos = getpos(".")
    " Apply movement
    normal! w
    " Get cursor potential next position
    let wcurpos = getpos(".")
    " Return cursor to original place
    call setpos(".", curpos)
    " Get the string between the two cursor positions
    let line = getline(line("."))
    if curpos[1] == wcurpos[1] " word within the same line
            let str = line[curpos[2]-1:wcurpos[2]-1]
    else
            " word motion goes to next line
            let str = line[curpos[2]-1:]
    endif
    " Look for upper case in the string
    let m = match(str, '[A-Z]', 1)
    " If upper case letter found
    if m >= 1
            exec "normal ". m ."l"
    else
            " else just move as normal
            normal! w
    endif
endfunction

nnoremap w :call Word()<cr>