Thorsten Lorenz
Thorsten Lorenz

Reputation: 11847

How can I CamelCase-enable Vim search?

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.

Upvotes: 19

Views: 578

Answers (2)

ib.
ib.

Reputation: 28964

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

Upvotes: 13

Gary Willoughby
Gary Willoughby

Reputation: 52498

There is a nice plugin called fuzzy finder that may be useful.

http://www.vim.org/scripts/script.php?script_id=1984

FuzzyFinder provides convenient ways to quickly reach the buffer/file/command/bookmark/tag you want. FuzzyFinder searches with a fuzzy/partial pattern such as camel case.

Upvotes: 0

Related Questions