├── README.md ├── autoload └── operator │ └── gsearch.vim └── plugin └── operator └── gsearch.vim /README.md: -------------------------------------------------------------------------------- 1 | # Custom Operator to Search Using Ag/Ack/CrlSF/etc 2 | 3 | ## Installation 4 | 5 | * Install [kana's operator plugin](https://github.com/kana/vim-operator-user) 6 | 7 | * Install this plugin 8 | 9 | ## Usage 10 | 11 | * Set up some custom mappings. For example: 12 | 13 | ``` 14 | map g/ (operator-ag) " or ack/ctrlsf 15 | map g? (operator-ggrep) 16 | map gw (operator-ag-word) 17 | map gh (operator-helpgrep) "or dash if you use that 18 | ``` 19 | 20 | * The most useful way to use this plugin is using the visual mode mapping. E.g. 21 | make a selection and hit `g/`. 22 | 23 | * You may also use motions. To search in parentheses: `g/i)`. 24 | 25 | ## Customization 26 | 27 | You can customize the Ag, Ack, CtrlSF, command if you'd like. The most common 28 | customizations would be: 29 | 30 | * Use regexes rather than treating search query as a literal. 31 | 32 | ``` 33 | let g:gsearch_ag_command = 'Ag!' 34 | ``` 35 | 36 | * To jump to the first result immediately. 37 | 38 | ``` 39 | let g:gsearch_ag_command = 'Ack -Q' 40 | ``` 41 | 42 | ## Acknowledgements 43 | 44 | * sjl's original tip: http://www.vimbits.com/bits/153. If you would like an 45 | implementation of this plugin without relying on user operators you're 46 | welcome to use and extend that. 47 | 48 | * [Angelic-sedition's](https://github.com/angelic-sedition/dotfiles/blob/master/vim/.vimrc#L1278) 49 | original implementation. My implementation doesn't rely on unite and on 50 | registers. 51 | -------------------------------------------------------------------------------- /autoload/operator/gsearch.vim: -------------------------------------------------------------------------------- 1 | " default values for settings {{{ 2 | if !exists("g:gsearch_ag_command") 3 | let g:gsearch_ag_command = 'Ag! -Q' 4 | endif 5 | 6 | if !exists("g:gsearch_ack_command") 7 | let g:gsearch_ack_command = 'Ack! -Q' 8 | endif 9 | 10 | if !exists("g:gsearch_ctrlsf_command") 11 | let g:gsearch_ctrlsf_command = 'CtrlSF' 12 | endif 13 | "}}} 14 | " operators {{{ 15 | function! operator#gsearch#ggrep(motion_wise) 16 | call s:search_cmd_quote('Ggrep', a:motion_wise) 17 | endfunction 18 | 19 | function! operator#gsearch#ag_word(motion_wise) 20 | call s:search_cmd_quote(g:gsearch_ag_command . ' -w', a:motion_wise) 21 | endfunction 22 | 23 | function! operator#gsearch#ag(motion_wise) 24 | call s:search_cmd_quote(g:gsearch_ag_command, a:motion_wise) 25 | endfunction 26 | 27 | function! operator#gsearch#ack(motion_wise) 28 | call s:search_cmd_quote(g:gsearch_ack_command, a:motion_wise) 29 | endfunction 30 | 31 | function! operator#gsearch#ctrlsf(motion_wise) 32 | call s:search_cmd_quote(g:gsearch_ctrlsf_command, a:motion_wise) 33 | endfunction 34 | 35 | function! operator#gsearch#dash(motion_wise) 36 | call s:search_cmd('Dash', a:motion_wise) 37 | endfunction 38 | 39 | function! operator#gsearch#helpgrep(motion_wise) 40 | call s:search_cmd('helpgrep', a:motion_wise) 41 | endfunction 42 | "}}} 43 | " internal {{{ 44 | function! s:search_cmd_quote(cmd, motion_wise) 45 | execute a:cmd . ' ' . s:ag_quote(s:operator_sel(a:motion_wise)) 46 | endfunction 47 | 48 | function! s:search_cmd(cmd, motion_wise) 49 | execute a:cmd . ' ' . fnameescape(s:operator_sel(a:motion_wise)) 50 | endfunction 51 | 52 | function! s:operator_sel(motion_wise) 53 | let v = operator#user#visual_command_from_wise_name(a:motion_wise) 54 | let [save_reg_k, save_regtype_k] = [getreg('k'), getregtype('k')] 55 | try 56 | execute 'normal!' '`[' . v . '`]"ky' 57 | " return s:get_visual_selection() 58 | return getreg('k') 59 | finally 60 | call setreg('k', save_reg_k, save_regtype_k) 61 | endtry 62 | endfunction 63 | 64 | " Stolen from: http://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript 65 | " currently not used as we are pasting to a random register instead 66 | function! s:get_visual_selection() 67 | let [lnum1, col1] = getpos("'<")[1:2] 68 | let [lnum2, col2] = getpos("'>")[1:2] 69 | let lines = getline(lnum1, lnum2) 70 | let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] 71 | let lines[0] = lines[0][col1 - 1:] 72 | return join(lines, "\n") 73 | endfunction 74 | "}}} 75 | 76 | function! s:ag_quote(str) 77 | " This was old escaping mechanism 78 | " return shellescape(fnameescape(str)) 79 | return escape(fnameescape(escape(a:str, '#%')), '()') 80 | endfunction 81 | -------------------------------------------------------------------------------- /plugin/operator/gsearch.vim: -------------------------------------------------------------------------------- 1 | if exists('g:loaded_operator_search') 2 | finish 3 | endif 4 | 5 | call operator#user#define('ag', 'operator#gsearch#ag') 6 | call operator#user#define('ag-word', 'operator#gsearch#ag_word') 7 | call operator#user#define('ack', 'operator#gsearch#ack') 8 | call operator#user#define('ggrep', 'operator#gsearch#ggrep') 9 | call operator#user#define('ctrlsf', 'operator#gsearch#ctrlsf') 10 | call operator#user#define('helpgrep', 'operator#gsearch#helpgrep') 11 | call operator#user#define('dash', 'operator#gsearch#dash') 12 | 13 | let g:loaded_operator_search = 1 14 | --------------------------------------------------------------------------------