Search code examples
vimeditor

Is it possible create a shortcut on vim to replace multiple times on a file?


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.


Solution

  • 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 /