23

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?

25

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:

enter image description here

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:

enter image description here

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

UPDATE 03 Aug 2016

Another way to do line search is to use fzf along with accompanying vim plugin. See the installation instructions here: https://github.com/junegunn/fzf.vim#installation

After you have installed both fzf and fzf.vim, you can use :BLines to search lines in the current buffer:

Sample output

You may have noticed it's not exactly fuzzy search meaning that I need to use spaces. This may not work you.

2
  • Added unite as an alternative solution.
    – baltazar
    Feb 27 '14 at 9:21
  • Added fzf as an alternative solution.
    – baltazar
    Aug 3 '16 at 10:50
1

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.

1
  • Ah, yes. incsearch is great, but not what I'm looking for.
    – adamesque
    Feb 15 '12 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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