From 52c3b26195508150a3ffaed0ba8c493f5999a519 Mon Sep 17 00:00:00 2001 From: Urbain Vaes Date: Thu, 1 Feb 2018 14:10:43 +0000 Subject: [vim] Add find and git ls-files to custom search --- vim/.vimrc | 84 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 32 deletions(-) (limited to 'vim/.vimrc') diff --git a/vim/.vimrc b/vim/.vimrc index 816fba5..a7a0981 100644 --- a/vim/.vimrc +++ b/vim/.vimrc @@ -124,11 +124,11 @@ nnoremap gd :Gdiff " Heytmux function! Call_heytmux(vm) - call feedkeys(":'[,']Heytmux ") + call feedkeys(":'[,']Heytmux! ") endfunction nmap gh :set opfunc=Call_heytmuxg@ -xmap gh :'<,'>Heytmux +xmap gh :Heytmux! " Iron let g:iron_map_defaults=0 @@ -370,42 +370,62 @@ augroup END " }}} "" My search {{{ -if executable("ag") - set grepprg=ag\ -uu\ --vimgrep - set grepformat=%f:%l:%c:%m -endif - -if executable("rg") - set grepprg=rg\ -uu\ --vimgrep - set grepformat=%f:%l:%c:%m -endif -command! -nargs=+ -complete=file_in_path Grep execute 'silent grep!' | cw | redraw! +" http://vim.wikia.com/wiki/Searching_for_files +" 1 argument: set search program and options +" 2 argument: do search +function! MySearch(...) + let l:prevgrepprg=&grepprg + let l:prevgrepformat=&grepformat + if a:1 == "Rg" + set grepprg=rg\ -uu\ --vimgrep + set grepformat=%f:%l:%c:%m + elseif a:1 == "Gnufind" + set grepprg=find\ .\ -name + set grepformat=%f + elseif a:1 == "Gitfind" + set grepprg=git\ ls-files\ -i\ -x + set grepformat=%f + endif + execute 'silent grep!' a:2 | cwindow | redraw! + let &grepprg=l:prevgrepprg + let &grepformat=l:prevgrepformat +endfun + +command! -nargs=+ -complete=file_in_path Rg call MySearch("Rg", ) +command! -nargs=+ -complete=file_in_path Gnufind call MySearch("Gnufind", ) +command! -nargs=+ -complete=file_in_path Gitfind call MySearch("Gitfind", ) command! -nargs=+ -complete=file_in_path GitGrep execute 'silent Ggrep!' | cw | redraw! command! -nargs=+ -complete=file_in_path VimGrep execute 'silent vimgrep!' | cw | redraw! -function! My_search(vm) - let is_visual=(a:vm == "v") - let l=getline(is_visual ? "'<" : "'[") - let [line1,col1] = getpos(is_visual ? "'<" : "'[")[1:2] - let [line2,col2] = getpos(is_visual ? "'>" : "']")[1:2] - call feedkeys(':' . g:my_searchprg . ' "' . l[col1 - 1: col2 - 1] . '"') -endfunction - -let g:my_searchprg = "Grep" -function! Cycle_searchprg() - if g:my_searchprg == "Grep" - let g:my_searchprg = "GitGrep" - elseif g:my_searchprg == "GitGrep" - let g:my_searchprg = "VimGrep" - elseif g:my_searchprg == "VimGrep" - let g:my_searchprg = "Grep" +" Default search and find programs +function! FillSearch(...) + if a:0 == 0 + let l:filltext = "" + else + let l:is_visual=(a:1 == "v") + let l:line=getline(l:is_visual ? "'<" : "'[") + let [line1,col1] = getpos(l:is_visual ? "'<" : "'[")[1:2] + let [line2,col2] = getpos(l:is_visual ? "'>" : "']")[1:2] + let l:filltext =l:line[col1 - 1: col2 - 1] endif - echom g:my_searchprg + call feedkeys(':' . g:my_fillprg . ' "' . l:filltext . '"'."\") endfunction -nmap co/ :call Cycle_searchprg() -nmap g/ :set opfunc=My_searchg@ -xmap g/ :call My_search(visualmode()) +cmap setSearchMode let g:my_fillprg=g:my_searchprgs[g:my_searchprg] +cmap setFindMode let g:my_fillprg=g:my_findprgs[g:my_findprg] +nmap g/ :setSearchMode:set opfunc=FillSearchg@ +nmap ,g :setSearchMode:call FillSearch() +xmap ,g :setSearchMode:call FillSearch(visualmode()) +nmap ,f :setFindMode:call FillSearch() + +let g:my_searchprgs = ['Rg', 'GitGrep', 'VimGrep'] +let g:my_findprgs = ['Gnufind', 'Gitfind'] +let g:my_searchprg = 0 +let g:my_findprg = 0 + +" Cycle search / find prgs +nmap cog :let g:my_searchprg=(g:my_searchprg+1)%len(g:my_searchprgs):echom g:my_searchprgs[g:my_searchprg] +nmap cof :let g:my_findprg=(g:my_findprg+1)%len(g:my_findprgs):echom g:my_findprgs[g:my_findprg] " }}} -- cgit v1.2.3