Skip to content

Instantly share code, notes, and snippets.

@raghur
Last active December 20, 2015 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raghur/6081804 to your computer and use it in GitHub Desktop.
Save raghur/6081804 to your computer and use it in GitHub Desktop.
Vim script to easily find in files - some improvements over http://vim.wikia.com/wiki/Find_in_files_within_Vim
let s:grepopts='\ --exclude-dir=packages\ --exclude-dir=.git\ --exclude-dir=.svn\ --exclude-dir=tmp\ --exclude=*.intellisense.js\ --exclude=*-vsdoc.js\ --exclude=*.tmp\ --exclude=*.min.js\ -PHIirn\ $*'
if has('win32')
let s:find=fnamemodify(findfile("find.exe", $GNUWIN."**"), ":p")
let s:grep=fnamemodify(findfile("grep.exe", $GNUWIN."**"), ":p")
endif
execute "set grepprg=" . s:grep ."\\ " . s:grepopts
fun! Get_grep_include_opt(prefix)
let l:cmd = ""
if (expand("%:e") != "")
"let l:cmd = " --include=*.". expand("%:e") . " "
let l:cmd = a:prefix . expand("%:e") . " "
endif
return l:cmd
endfun
fun! Grep_with_args(patt, path)
let l:cmd=":silent lgrep! "
let l:post="\"" . a:patt . "\""
let l:pipe = "\| lopen"
let l:cmd = l:cmd . Get_grep_include_opt(" --include=*.")
let l:cmd = l:cmd . " " . l:post
if a:path != ""
let l:cmd = l:cmd . " " . a:path
else
let l:cmd = l:cmd . " *"
endif
let l:cmd = l:cmd . " " . l:pipe
return l:cmd
endfun
fun! s:get_visual_selection()
let l=getline("'<")
let [line1,col1] = getpos("'<")[1:2]
let [line2,col2] = getpos("'>")[1:2]
return l[col1 - 1: col2 - 1]
endfun
" lvimgrep - internal - slow
nnoremap <expr> <leader>* ":silent lvimgrep /" . expand("<cword>") . "/j " . Get_grep_include_opt("**/*.") . " \|lopen"
vnoremap <script> <leader>* <Esc>:lvimgrep /<C-R><C-R>=<SID>get_visual_selection()<CR>/j <C-R><C-R>=Get_grep_include_opt("**/*.")<CR>\|lopen
" vimgrep - fast but external
" project root
nnoremap <expr><leader>f Grep_with_args("\\b".expand("<cword>")."\\b", "")
vnoremap <script><leader>f <Esc>:silent lgrep
\ <C-R><C-R>=Get_grep_include_opt(" --include=*.")<CR>
\ "<C-R><C-R>=<SID>get_visual_selection()<CR>"
\ * \|lopen
" down current folder
nnoremap <expr><leader>fd Grep_with_args("\\b".expand("<cword>")."\\b", expand("%:p:h"))
vnoremap <script><leader>fd <Esc>:silent lgrep
\ <C-R><C-R>=Get_grep_include_opt(" --include=*.")<CR>
\ "<C-R><C-R>=<SID>get_visual_selection()<CR>"
\ <C-R><C-R>=expand("%:p:h")<CR>\* \|lopen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment