Vim plugin providing operator motions to quickly replace text
Subversive provides two new operator motions to make it very easy to perform quick substitutions. It contains no default mappings and will have no effect until you add your own maps to it.
Also see here if you prefer watching a video explanation of this plugin instead of the below text.
" s for substitute nmap s (SubversiveSubstitute) nmap ss (SubversiveSubstituteLine) nmap S (SubversiveSubstituteToEndOfLine)
Note that in this case you will be shadowing the change character key
sso you will have to use the longer form
After adding these maps, you can then execute
sto substitute the text object provided by the motion with the contents of the default register (or an explicit register if provided). For example, you could execute
siwto replace the current word under the cursor with the current yank, or
sipto replace the paragraph, etc.
Another interesting operator provided by subversive allows specifying both the text to replace and the line range over which to apply the change by using multiple consecutive motions.
nmap s (SubversiveSubstituteRange) xmap s (SubversiveSubstituteRange)
nmap ss (SubversiveSubstituteWordRange)
After adding this map, if you execute
sthen enter some text into a prompt in the status bar, then the text given by
motion1should be replaced by the text we entered in the prompt for each line provided by
motion2. Alternatively, we can also select
motion1in visual mode and then hit
sfor the same effect.
This can be very powerful. For example, you could execute
siwipto replace all instances of the current word under the cursor that exist within the paragraph under the cursor. Or
sl_to replace all instances of the character under the cursor on the current line.
ssmapping is used as a shortcut to replace the current word under the cursor. This allows you for example to execute
ssipto replace the word under cursor in the current paragraph. Note that this matches complete words so is different from
siwip(which will not require that there be word boundaries on each match)
Let's see it in action:
In this gif, we first rename the local
fooparameter by executing
barin the prompt (note that
omis a custom motion that stands for 'outer c# method' and is not provided by this plugin). Also note that because we are using
ss, the text
_foosis unaffected because it does not match the complete word. It is useful in this case because we only want to rename the parameter within the function.
After that we switch to visual mode and select the
sieand once again enter
barinto the prompt.
ieis again a custom motion that stands for
entire buffer(see next section for details)
After that we move to the
seieand once again enter
Bar. Then finally do the same for the fully capitalized
Note that if you are using neovim you can enable the
inccommandoption to get a real time preview of the replacement as you're typing it (equivalent to setting
Note also that this motion is repeatable. If you run
siwipin one paragraph, then go to a different paragraph, then hit
.then the same substitution will apply to the new paragraph under the cursor.
Note that to really take advantage of these mappings, it is helpful to add custom text objects in addition to just the built-in ones like current paragraph (
ip), current sentence (
is), or current line (
_). Custom text objects such as current indent level, current method, current class, entire buffer, current scroll page, etc. can all help a lot here.
For example, a couple really simple motions that are useful for subversive are:
" ie = inner entire buffer onoremap ie :exec "normal! ggVG"
" iv = current viewable text in the buffer onoremap iv :exec "normal! HVL"
There are also other plugins that provide lots of custom text objects that I would recommend taking a look at.
For many substitutions, you can rely on the highlight preview to understand what is being replaced. But if you are doing a larger replacement across the entire file you might want to confirm each one. You can do this with the following maps:
nmap cs (SubversiveSubstituteRangeConfirm) xmap cs (SubversiveSubstituteRangeConfirm) nmap css (SubversiveSubstituteWordRangeConfirm)
These work the same as the
smaps above except will step through each replacement one by one.
If you provide an explicit register to any of the substitute motions above it will not prompt and instead will use the contents of the given register. For example,
"asiwipwill immediately replace all instances of the current word under the cursor with the contents of register
athat exist within the current paragraph.
If this isn't enough, you can also use the following plugs instead:
nmap s (SubversiveSubstituteRangeNoPrompt) xmap s (SubversiveSubstituteRangeNoPrompt)
nmap ss (SubversiveSubstituteWordRangeNoPrompt)
Which will work identically to the previous plugs except instead of prompting it will use the default register.
You can do this by passing the black hole register to one of the substitute over range commands. For example:
"_csiwipif you want to confirm each delete.
Unfortunately you cannot just input an empty value into the prompt because there isn't a way to distinguish this input from cancelling via escape (unless you set
1in which case that will work fine)
g:subversivePromptWithCurrent- When set to
1, the prompt will include the text that is being replaced. This can be useful if you want to just make an edit to it. Default:
g:subversiveCurrentTextRegister- When set, the given register will be populated with the text that is being replaced. This can be useful as an alternative to
g:subversivePromptWithCurrent, so that you can hit
rin the prompt (assuming you set it to
'r') when you want to edit it and otherwise just directly type when you want to do a full replace.
g:subversivePromptWithActualCommand- When set to
1, instead of only prompting for the replacement text, it will prompt with the native vim substitute command. Veteran vim users might prefer this so that they can adjust the various substitution flags themselves. Default:
g:subversivePreserveCursorPosition- When set to
1, the cursor will not move when substitutions are applied. Default is
0to match normal vim behaviour.
If you have also installed vim-abolish, then you might consider adding something similar to the following mapping as well:
nmap s (SubversiveSubvertRange) xmap s (SubversiveSubvertRange)
nmap ss (SubversiveSubvertWordRange)
This will behave the same as
sexcept that it will perform an abolish 'subvert' instead of using vim's built in substitute command. This will apply the substitution and also preserve whatever case the original word has. For example:
In this case, we move the cursor overtop
fooand then execute
bar, which replaces all instances of
fooregardless of case.
This can be a very convenient way to perform quick renames.
As you would expect, the
ssmapping works similarly except only matches complete words that include word boundaries.
And once again there are also alternative plugs that will use default register instead of a prompt if you prefer that:
nmap s (SubversiveSubvertRangeNoPrompt) xmap s (SubversiveSubvertRangeNoPrompt)
nmap ss (SubversiveSubvertWordRangeNoPrompt)
Note that if you install vim-yoink alongside vim-subversive, then the post-paste yoink swapping feature will automatically work with subversive (single motion) substitutions as well. In other words, assuming the default mappings, you can execute
siwthen hit / to swap between different yanks from the yoink history.
Subversive also provides a plug to replace visual mode paste to provide post paste swapping there as well:
xmap s (SubversiveSubstitute) xmap p (SubversiveSubstitute) xmap P (SubversiveSubstitute)
Now if you hit
pwhile in visual mode you can swap between yanks just like when pasting in normal mode.