1

When I select a section of text within a line, then indicate I want to substitute on that line with :s, the result is that the entire line is affected.

In order to actually do what I intend, I have to add \%V before and after the string I want to substitute within the selection, so that no other similar strings are substituted outside of the selection on that line.

Is there a way to configure Vim 8 to behave as I expect it to? That is, if I go to the trouble of selecting text, and I indicate that I want to substitute one thing for another, the substitution takes place within the selection by default.

5

1 Answer 1

3

That is because as all ex commands work linewise.

However you might be able to achieve something desired using an mapping for visual mode. The following maps the :s command in visual mode and appends the \%V regex atom:

 :xnoremap :s :s/\%V

(Unfortunately, there doesn't seem to exists an :xabbrev command). Note: I don't think it makes sense to add a second \%V. Mainly because the regular expression might contain items, that may not need to be included within the last visual selection (think of using e.g. \ze or look-around assertions), at least I wouldn't want to have it appended a second time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.