Skip to content

Instantly share code, notes, and snippets.

@Bakudankun
Last active March 16, 2023 12:47
Show Gist options
  • Save Bakudankun/2ccdf7f8f992ae2f7f5135d6d12bc948 to your computer and use it in GitHub Desktop.
Save Bakudankun/2ccdf7f8f992ae2f7f5135d6d12bc948 to your computer and use it in GitHub Desktop.
Incremental fuzzy search for Vim/Neovim
function s:infs(...)
call setloclist(0, [], ' ', #{title: 'infs: ', quickfixtextfunc: function('s:infs_text')})
lopen
wincmd p
redraw
augroup infs-input
autocmd CmdlineChanged @ call s:udpate_loclist(getcmdline())
augroup END
call input('query: ', get(a:, '1', ''))
autocmd! infs-input
if getloclist(0)->len() > 0
lopen
else
lclose
endif
endfunction
function s:udpate_loclist(query) abort
if getchar(1) !=# 0
return
endif
const bufnr = bufnr()
call setloclist(0, [], 'r', #{
\ title: 'infs: ' .. a:query,
\ items: getline(1, '$')
\ ->map({ i, v -> #{text: v, bufnr: bufnr, lnum: i + 1} })
\ ->matchfuzzy(a:query, #{key: 'text'})
\ })
redraw
endfunction
function s:infs_text(info) abort
let items = getloclist(a:info.winid, #{id: a:info.id, items: 1}).items
return items[a:info.start_idx - 1 : a:info.end_idx - 1]
\ ->map({_, v -> printf('|%3d| %s', v.lnum, v.text->substitute('^\s*', '', ''))})
endfunction
command -nargs=? Infs call s:infs(<f-args>)
@Bakudankun
Copy link
Author

Bakudankun commented Mar 15, 2023

Install

Place infs.vim into ~/.vim/plugin or ~/.config/nvim/plugin.

Usage

:Infs [{initial query}]

License

CC0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment