Skip to content

Instantly share code, notes, and snippets.

@lingceng
Last active August 29, 2015 14:04
Show Gist options
  • Save lingceng/efffc52cb31594efba85 to your computer and use it in GitHub Desktop.
Save lingceng/efffc52cb31594efba85 to your computer and use it in GitHub Desktop.
grep operator vim plugin, grep text recursily
" out of date
" see https://github.com/lingceng/lingceng-config
"
" Grep Operator Vim Plugin
"
" Description:
" Grep text recursively in current file and open the quickfix window.
"
" Install:
" Put this plugin here ~/.vim/plugin/grep-operator.vim
"
" Usage:
" <leader>g
" "leader usually be \
"
" Example:
" "grep current word
" \giw
"
nnoremap <leader>g :set operatorfunc=<SID>GrepOperator<cr>g@
" ctrl-u in command mean delete from the cursor to the beginning of the line
vnoremap <leader>g :<c-u>call <SID>GrepOperator(visualmode())<cr>
" s means put function in current script namespace
" ! means silent replace exist function
function! s:GrepOperator(type)
" cache the register
let saved_unnamed_register = @@
if a:type ==# 'v'
" copy selected in visual mode
execute "normal! `<v`>y"
elseif a:type ==# 'char' " ==# case-sensitive compare
" copy in <leader>gw format
execute "normal! `[v`]y"
else
return
endif
" @ is the unnameed default register
silent execute "grep! -R " . shellescape(@@) . " . "
copen
let @@ = saved_unnamed_register
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment