How can I CamelCase-enable Vim search?

The described functionality can be easily implemented by means of Vimscript. Let us consider the following custom mappings.

nnoremap <expr> <leader>/ SearchCamelCase('/')
nnoremap <expr> <leader>? SearchCamelCase('?')

function! SearchCamelCase(dir)
    call inputsave()
    let ab = input(a:dir)
    call inputrestore()

    let l = filter(split(toupper(ab), '\zs'), 'v:val =~ "\\w"')
    if len(l) > 0
        let l[0] = '[' . l[0] . tolower(l[0]) . ']'
    end
    let @/ = '\C\<' . join(map(l, 'v:val . "[0-9a-z_]*"'), '') . '\>'

    return a:dir . "\r"
endfunction

Tags:

Vim