Skip to content

Instantly share code, notes, and snippets.

@dahu
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dahu/9718037 to your computer and use it in GitHub Desktop.
Save dahu/9718037 to your computer and use it in GitHub Desktop.
Combine the power of [I and :ilist /pattern/ in Vim
function! IList()
let term = input("IList: /")
if term == ''
let term = expand('<cword>')
endif
let v:errmsg = ''
redir =>slist
exe 'ilist /' . term
redir END
if v:errmsg == ''
let llist = split(slist, "\n") " remove \@ nuls
let slist = join(llist, "\n")
call filter(llist, 'v:val =~ "^\\s\\+\\d\\+:"')
let choice = str2nr(input('Find: '))
call histdel('input', -1) " remove numeric choice leaving named term as last
if (choice > 0) && (choice <= len(llist))
let line = matchstr(llist[choice-1], '^\s*\d\+:\s\+\zs\d\+')
let file = split(matchstr(slist, '\_.*\%(\_^\|\n\)\zs\S\+\ze\_.\{-}\n\s\+' . choice))[0] " remove \@ nul
if bufexists(file)
exe 'buffer ' . file
else
exe 'edit ' . file
endif
exe line
normal! 0
call search(term, 'c')
endif
else
let v:errmsg .= ': ' . term
endif
endfunction
nnoremap [I :call IList()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment