Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I CamelCase-enable Vim search?

Tags:

vim

Is there a Vim plugin, script, or function out there that allows Vim search to be extended in order to match camel-cased words when I type its capital letters in succession?

Here is an example to clarify. Let’s say I am looking for WordInQuestion. I would like to be able to just type /wiq in order to find it.

As an added bonus, it would be nice if I could find getWordInQuestion by typing /gwiq which means the first letter of the word I am looking for may be lower case.

like image 722
Thorsten Lorenz Avatar asked Dec 30 '11 02:12

Thorsten Lorenz


1 Answers

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
like image 186
ib. Avatar answered Sep 21 '22 00:09

ib.