Consider this string:1 1 1When I mark the first two 1s in visual mode and then :'<,'>s/1/2/g, all 1s on the line are replaced by 2s, not just those in the selected region.  Why is this the case and how can I restrict the operation to the selected text?
Why is substitution applied outside the selected region?
visual mode;substitute
The :s[ubstitue] command operates on whole lines and the [range] prefix is a range of lines.To restrict the operation to just the visually-selected region, you can use the \%V atom in your search pattern. That is, visually-select the first two 1s in your example, then use this command to replace just those 1s:'<,'>s/\%V1/2/gSee also:help /\%VThe :help entry says to put \%V at the start and end of the pattern, but I didn't read that before trying it and it worked in the example here with \%V at just the start of the pattern.