Skip to content

Instantly share code, notes, and snippets.

@bf4648
Forked from jinzhubaofu/vim find
Created June 26, 2021 18:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bf4648/caee199145f7060833d06d6da339e663 to your computer and use it in GitHub Desktop.
vim find from hades
'Find' : 'find')<CR>
" Find file in current directory and edit it.
function! Find(name)
let l:list=system("find . -type f | grep -i '".a:name."' | perl -ne 'print \"$.\\t$_\"'")
" let l:list=system("find . -iname '".a:name."' | perl -ne 'print \"$.\\t$_\"'")
" replace above line with below one for gvim on windows
" let l:list=system("find . -name ".a:name." | perl -ne \"print qq{$.\\t$_}\"")
let l:num=strlen(substitute(l:list, "[^\n]", "", "g"))
if l:num < 1
echo "'".a:name."' not found"
return
endif
if l:num != 1
echo l:list
let l:input=input("Which ? (CR=nothing)\n")
if strlen(l:input)==0
return
endif
if strlen(substitute(l:input, "[0-9]", "", "g"))>0
echo "Not a number"
return
endif
if l:input<1 || l:input>l:num
echo "Out of range"
return
endif
let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*")
else
let l:line=l:list
endif
let l:line=substitute(l:line, "^[^\t]*\t./", "", "")
execute ":e ".l:line
endfunction
command! -nargs=1 Find :call Find("<args>")
:cabbrev find <c-r>=(getcmdtype()==':' && getcmdpos()==1 ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment