Reputation: 1240
I would like create a shortcut on vim to replace multiple times on a file, this a tool that I use several time.
Example of tool:
:%s/this.props/otherProps/gc
This command will replace all this.props to otherProps and ask to confirm
But I'm trying to convert this command in a shortcut like the example below:
// on .vimrc
noremap <leader>F :%s/$1/$2/gc
So in this case, I would every time a press <leader>
+ F
, I type the first word, and after I press tab
and type second word and after press enter.
I've put in the $1
and $2
as an attempt, but it doesn't work.
I tried to search some document about this, but I didn't find anything, maybe have some word
more specific about that I don't know yet.
Upvotes: 1
Views: 392
Reputation: 1
cnoreabbrev <expr> esr getcmdtype() == ":" && getcmdline() == 'esr' ? '%s:\<end\>: & /*}*/:g' : 'esr'
This replaces end to end: /*}*/
You can add this to gvimrc
; whenever :esr
is typed, the above will be executed.
Upvotes: 0
Reputation: 1244
Whilst this doesn't do exactly what you asked for, it does get pretty close.
noremap <leader>F :%s//gc<LEFT><LEFT><LEFT>
This will type out the command noremap <leader>F :%s//gc
but leave your cursor between the 2 slashes, ready to type in the stuff you want to find and replace.
So now you can just hit <leader>F
and start typing this.props/otherprops
and then hit enter. It does exactly what you want but instead of pressing tab
you press /
Upvotes: 3
Reputation: 2752
You could use a custom function by adding something like the following to your .vimrc
:
function! Replace()
call inputsave()
let pattern = input('Enter pattern:')
let replacement = input('Enter replacement:')
call inputrestore()
execute '%s/'.pattern.'/'.replacement.'/gc'
endfunction
nnoremap <leader>f :call Replace()<cr>
With this everytime you hit <leader>f
the function is called. Which is then gonna ask you first for a pattern, then for a replacement and is then gonna execute the substitute command.
Upvotes: 5