Created
June 23, 2010 08:11
-
-
Save h1mesuke/449628 to your computer and use it in GitHub Desktop.
Vim - My keymaps for searching and replacing text
This file contains 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
"--------------------------------------- | |
" Replace | |
" eregex.vim | |
" http://www.vector.co.jp/soft/unix/writing/se265654.html | |
" NOTE: :S and :M commands accept Perl-compatible regexp notation | |
let g:eregex_meta_chars = '^$()|[]{}.*+?\/' | |
let g:vregex_meta_chars = '^$|[].*\/' | |
" line | |
nnoremap s :<C-u>S//g<Left><Left> | |
nnoremap <expr> ,ls ':<C-u>S/\<' . expand('<cword>') . '\>//g<Left><Left>' | |
nnoremap <expr> ,lg ':<C-u>S/' . expand('<cword>') . '//g<Left><Left>' | |
" buffer | |
nnoremap S :<C-u>%S//g<Left><Left> | |
nnoremap <expr> ,s ':<C-u>%S/\<' . expand('<cword>') . '\>//g<Left><Left>' | |
nnoremap <expr> ,g ':<C-u>%S/' . expand('<cword>') . '//g<Left><Left>' | |
vnoremap ,s "vy:%S/\<<C-r>=substitute(escape(@v,g:eregex_meta_chars),"\n",'\\n','g')<CR>\>//g<Left><Left> | |
vnoremap ,g "vy:%S/<C-r>=substitute(escape(@v,g:eregex_meta_chars),"\n",'\\n','g')<CR>//g<Left><Left> | |
" selected | |
vnoremap s :S//g<Left><Left> | |
nnoremap ,vS :<C-u>%S/\%V/g<Left><Left> | |
nnoremap <expr> ,vs ':<C-u>%S/\%V\<' . expand('<cword>') . '\>//g<Left><Left>' | |
nnoremap <expr> ,vg ':<C-u>%S/\%V' . expand('<cword>') . '//g<Left><Left>' | |
" buffers | |
nnoremap <Leader>S :<C-u>bufdo %S//g<Left><Left> | |
nnoremap <expr> <Leader>s ':<C-u>bufdo %S/\<' . expand('<cword>') . '\>//g<Left><Left>' | |
nnoremap <expr> <Leader>g ':<C-u>bufdo %S/' . expand('<cword>') . '//g<Left><Left>' | |
vnoremap <Leader>s "vy:bufdo %S/\<<C-r>=substitute(escape(@v,g:eregex_meta_chars),"\n",'\\n','g')<CR>\>//g<Left><Left> | |
vnoremap <Leader>g "vy:bufdo %S/<C-r>=substitute(escape(@v,g:eregex_meta_chars),"\n",'\\n','g')<CR>//g<Left><Left> | |
"--------------------------------------- | |
" Search | |
nnoremap / :<C-u>set ignorecase smartcase<CR>/ | |
nnoremap ? :<C-u>set ignorecase smartcase<CR>? | |
nnoremap n nzzzv | |
nnoremap N Nzzzv | |
" borrowed from: | |
" http://vim-users.jp/2009/10/hack91/ | |
cnoremap <expr> / getcmdtype() == '/' ? '\/' : '/' | |
cnoremap <expr> ? getcmdtype() == '?' ? '\?' : '?' | |
" derived from: | |
" http://vim-users.jp/2009/11/hack104/ | |
vnoremap <silent> * "vy/\<<C-r>=substitute(escape(@v,g:vregex_meta_chars),"\n",'\\n','g')<CR>\><CR>N | |
vnoremap <silent> g* "vy/<C-r>=substitute(escape(@v,g:vregex_meta_chars),"\n",'\\n','g')<CR><CR>N | |
" borrowed from: | |
" http://vim-users.jp/2009/08/hack65/ | |
vnoremap z/ <Esc>:set ignorecase smartcase<CR>/\%V | |
vnoremap z? <Esc>:set ignorecase smartcase<CR>?\%V | |
" clear highlights | |
nnoremap <silent> <C-l> :<C-u>noh<CR><C-l>:set noignorecase<CR> | |
" grep | |
command! -complete=file -nargs=+ -bang Grep call <SID>grep([<f-args>], '<bang>') | |
nnoremap <Space>G :Grep<Space> | |
nnoremap <silent><expr> <Space>g* '*:Grep! ' . expand('<cword>') . '<CR>' | |
nnoremap <silent><expr> <Space>gg 'g*:Grep ' . expand('<cword>') . '<CR>' | |
vmap <silent> <Space>g* *:Grep <C-r>=substitute(escape(@v,g:vregex_meta_chars),"\n",'\\n','g')<CR><CR> | |
" derived from: | |
" http://vim-users.jp/2010/03/hack130/ | |
function! s:grep(args, bang) | |
let pattern = a:args[0] | |
if a:bang == '!' | |
let pattern = '\<' . pattern . '\>' | |
endif | |
if len(a:args) == 1 | |
let files = "**/*" | |
else | |
let files = join(a:args[1:]) | |
endif | |
silent! execute 'vimgrep' '/' . pattern . '/j ' files | |
if !empty(getqflist()) | |
copen | |
endif | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment