Created
October 18, 2010 05:02
-
-
Save h1mesuke/631739 to your computer and use it in GitHub Desktop.
Vim - My vimrc snippet for searching for texts; ver.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"--------------------------------------- | |
" Search | |
set incsearch nohlsearch | |
set noignorecase nosmartcase | |
nnoremap [Space]hl :<C-u>call util#toggle_option("hlsearch") | |
cnoremap <expr> / getcmdtype() == '/' ? '\/' : '/' | |
cnoremap <expr> ? getcmdtype() == '?' ? '\?' : '?' | |
function! s:pre_search_command() | |
" return ":\<C-u>set hlsearch ignorecase smartcase\<CR>" | |
return ":\<C-u>set hls ic scs\<CR>" | |
endfunction | |
function! s:post_jump_command() | |
return 'zz' | |
endfunction | |
execute 'nnoremap <silent> n' s:pre_search_command() . 'n' . s:post_jump_command() | |
execute 'nnoremap <silent> N' s:pre_search_command() . 'N' . s:post_jump_command() | |
" used only for setting the pattern | |
execute 'nnoremap <silent> *' s:pre_search_command() . '*N' | |
execute 'nnoremap <silent> #' s:pre_search_command() . '#N' | |
execute 'nnoremap <silent> g*' s:pre_search_command() . 'g*N' | |
execute 'nnoremap <silent> g#' s:pre_search_command() . 'g#N' | |
execute 'xnoremap <silent> * "vy' s:pre_search_command() . '/<C-r>=<SID>reg_v_value()<CR><CR>N' | |
execute 'xnoremap <silent> # "vy' s:pre_search_command() . '?<C-r>=<SID>reg_v_value()<CR><CR>' | |
"" search for the user input in the current buffer | |
execute 'nnoremap /' s:pre_search_command() . '/' | |
execute 'nnoremap ?' s:pre_search_command() . '?' | |
" search for the user input in the selection | |
execute 'xnoremap ,v/' s:pre_search_command() . '/\%V' | |
execute 'xnoremap ,v?' s:pre_search_command() . '?\%V' | |
" search for the user input in the last selected | |
execute 'nnoremap ,v/' s:pre_search_command() . '/\%V' | |
execute 'nnoremap ,v?' s:pre_search_command() . '?\%V' | |
" clear the highlights | |
nnoremap <silent> <C-l> :<C-u>noh<CR><C-l>:set nohlsearch noignorecase nosmartcase<CR> | |
"--------------------------------------- | |
if !exists('g:grep_glob') | |
let g:grep_glob = '**/*' | |
endif | |
function! s:grep_glob_set(...) | |
if a:0 | |
let g:grep_glob = a:1 | |
else | |
let g:grep_glob = '**/*' | |
endif | |
endfunction | |
" derived from: | |
" http://vim-users.jp/2010/03/hack130/ | |
function! s:grep(args, ...) | |
if a:0 | |
let grep = 'vimgrepadd' | |
else | |
let grep = 'vimgrep' | |
endif | |
let pat = a:args[-1] | |
let globs = join(a:args[:-2]) | |
silent! execute grep '/'.pat.'/j' globs | |
if empty(getqflist()) | |
redraw | |
echo "No matches found." | |
else | |
let @/ = pat | |
set hlsearch | |
copen | |
endif | |
endfunction | |
command! -nargs=? Grepglob call s:grep_glob_set(<f-args>) | |
nnoremap <expr> [Space]glo ':Grepglob ' . g:grep_glob | |
command! -complete=file -nargs=+ Grep call s:grep([<f-args>]) | |
command! -complete=file -nargs=+ Grepadd call s:grep([<f-args>], 1) | |
" grep the user input | |
nnoremap [Space]gre :<C-u>Grep <C-r>=g:grep_glob<CR> | |
nnoremap [Space]Gre :<C-u>Grepadd <C-r>=g:grep_glob<CR> | |
" grep the cword | |
nnoremap <silent> [Space]g* :<C-u>Grep <C-r>=g:grep_glob<CR> \<<C-r>=expand('<cword>')<CR>\><CR> | |
nnoremap <silent> [Space]gg :<C-u>Grep <C-r>=g:grep_glob<CR> <C-r>=expand('<cword>')<CR><CR> | |
nnoremap <silent> [Space]G* :<C-u>Grepadd <C-r>=g:grep_glob<CR> \<<C-r>=expand('<cword>')<CR>\><CR> | |
nnoremap <silent> [Space]Gg :<C-u>Grepadd <C-r>=g:grep_glob<CR> <C-r>=expand('<cword>')<CR><CR> | |
" grep the selection | |
xnoremap <silent> [Space]g* "vy:<C-u>Grep <C-r>=g:grep_glob<CR> \<<C-r>=<SID>reg_v_value()<CR>\><CR> | |
xnoremap <silent> [Space]gg "vy:<C-u>Grep <C-r>=g:grep_glob<CR> <C-r>=<SID>reg_v_value()<CR><CR> | |
xnoremap <silent> [Space]G* "vy:<C-u>Grepadd <C-r>=g:grep_glob<CR> \<<C-r>=<SID>reg_v_value()<CR>\><CR> | |
xnoremap <silent> [Space]Gg "vy:<C-u>Grepadd <C-r>=g:grep_glob<CR> <C-r>=<SID>reg_v_value()<CR><CR> |
grep は別関数にした方がいいかなー
/ や ? と共通部分が多いから無理やりまとめてしまったけど、分岐が増えて全体の見通しが悪くなってしまった。
grep を別関数にするとともに、共通部分を関数にくくり出して replace とも共有
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
というか、gist にコメ欄があったことに始めて気付いたw
メモ的なことを書いておくのにも使えますね。