Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I love using the Command-T plugin in Vim to do fuzzy searches for filenames. I like it so much that I've started to want to do the same fuzzy searches for arbitrary strings within an open buffer.

For example, if I'm editing a CSS file that contains a selector like #support-main .question .answer-rating, I can currently type /support-main .question to find that line.

I'd like to be able to invoke a fuzzy finder and simply type something like supmaique to find that same line (among others). I know I can type /sup.*mai.*que for the same effect but typing the .*'s breaks my concentration.

Is there a way to do this?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

There is "line" extension in the latest Ctrl-P plugin for vim (ctrlp) which can do fuzzy line search. You need to enable the extension manually. Here is my config in .vimrc:

let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlPLastMode'
let g:ctrlp_extensions = ['buffertag', 'tag', 'line', 'dir']

After that you press Ctrl-p to bring menu, then press Ctrl-f several times until the line mode is on. Type your fuzzy string now: http://i.stack.imgur.com/w8MWV.jpg

UPDATE 27 Feb 2014

An alternative solution that I'm currently using myself would be to use unite. In order to do fuzzy line search you need to slightly tune unite:

call unite#filters#matcher_default#use(['matcher_fuzzy'])
call unite#filters#sorter_default#use(['sorter_rank'])
call unite#custom#source('file,file/new,buffer,file_rec,line', 'matchers', 'matcher_fuzzy')
nnoremap <C-k> :<C-u>Unite -buffer-name=search -start-insert line<cr>

Now press Ctrl-k and type: http://i.imgur.com/Y9YSDx8.png

By the way, unite can also do fuzzy file search by name.

share|improve this answer
    
Added unite as an alternative solution. –  baltazar Feb 27 at 9:21

Not exactly what you want, but with set incsearch your pattern is matched as you type. It's not really fuzzy matching but the feeling is sort of similar.

share|improve this answer
    
Ah, yes. incsearch is great, but not what I'm looking for. –  adamesque Feb 15 '12 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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