Reputation: 29096
I would like to map <c-h> to do an easy "replace" in the active buffer
For now I have something like this:
" <c-h> Replacement
noremap <c-h> :%s/<c-R><c-W>//g<left><left>
inoremap <c-h> <esc>:%s/<c-R><c-W>//g<left><left>
vnoremap <c-h> gv:%s/<c-R><c-W>//g<left><left>
The problems are:
Is there anyway to do something like:
inoremap <c-h> <esc>:%s/\=escapechars(<c-R><c-W>)}//g<left><left>
Upvotes: 0
Views: 389
Reputation: 196516
I have this function:
function! GetVisualSelection()
let old_reg = @v
normal! gv"vy
let raw_search = @v
let @v = old_reg
return substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction
which you could use like this:
xnoremap <c-h> :<C-u>%s/<C-r>=GetVisualSelection()<CR>//g<left><left>
The "escaping" part could be extracted into its own function:
function! Escape(stuff)
return substitute(escape(a:stuff, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction
which you could use like this:
nnoremap <c-h> :<C-u>%s/<C-r>=Escape(expand('<cword>'))<CR>//g<left><left>
inoremap <c-h> <esc>:%s/<C-r>=Escape(expand('<cword>'))<CR>//g<left><left>
Upvotes: 2