Skip to content

Instantly share code, notes, and snippets.

@h1mesuke
Created October 18, 2010 05:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h1mesuke/631739 to your computer and use it in GitHub Desktop.
Save h1mesuke/631739 to your computer and use it in GitHub Desktop.
Vim - My vimrc snippet for searching for texts; ver.2
"---------------------------------------
" 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>
@tyru
Copy link

tyru commented Oct 19, 2010

nnoremap [Space]hl :<C-u>call util#toggle_option("hlsearch")

nnoremap [Space]hl :<C-u>call util#toggle_option("hlsearch")<CR>

では?

@h1mesuke
Copy link
Author

あ、ほんとだw
よくやるミスです。直しました。ありがとうございます。

@h1mesuke
Copy link
Author

というか、gist にコメ欄があったことに始めて気付いたw
メモ的なことを書いておくのにも使えますね。

@h1mesuke
Copy link
Author

grep は別関数にした方がいいかなー
/ や ? と共通部分が多いから無理やりまとめてしまったけど、分岐が増えて全体の見通しが悪くなってしまった。

@h1mesuke
Copy link
Author

grep を別関数にするとともに、共通部分を関数にくくり出して replace とも共有

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment