Say I want to fix a mistake in the following php code:

$line1 = intval($line[$1]);
$line2 = intval($line[$2]);
$line3 = intval($line[$3]);

and do substitution on the first line with:

# getting the line fixed:  
$line1 = intval($line[1]);

I know that on the current line I can repeat the substitution command with & or :@: commands, but is it a command I can do perform repetition of substitution on the consequent two lines, hence specify an address range for the repetition?

  • Do you still have something open in your question? How can we help you further? If not maybe could you accept one of the answers. It allow the question to rest :-) May 5, 2023 at 10:35

4 Answers 4


I would do:


If you don't specify a search pattern or a replace string the previous one is used.


You should have provided a :help :range to :help :s in the first place:


Anyway, you can use :help :&, here, which also takes a range:



  • the range starts at the line below the current line, .+
  • and ends two lines below the current line, .+2




  • the line below becomes "current", .+;
  • the range starts at that current line, .,
  • and ends on the line below, .+.

Note that the current line is assumed by default to the period is optional:


I use the following (sensible) Visual-mode mapping for &:

xnoremap & :&&<cr>

(Note that the duplicate :&& preserves flags, too; I actually also have :nnoremap & :&&<cr>.)

So vj& or similar would work with this mapping. For an Ex command the ranges in other answers are to be preferred (though sometimes it is faster to type vj: or similar to create the range if the command does not have an operator counterpart).


When I find myself with a command to repeat that may not lend itself to easy repetition... I resort to macros, and I typically use the "q register for these quick macro situations: qq

The macro would consist of the following :

  • colon to enter command mode
  • maybe with one or two characters of the command I want so arrowing up is efficient.
  • arrow up to the command and press enter
  • move down one line

(then another q to stop recording)

On my mac VIM that recorded into: :s/<80>ku^Mj (:reg to view register contents)

(I think the "<80>ku" corresponds with the up-arrow)

Then once recorded, I would use a repeat to execute it on how many ever subsequent lines you have: 2@q or 50@q

  • I tend to try to avoid history recalls in macros, as they can break in subtle ways when you execute other commands between invocations
    – D. Ben Knoble
    Apr 30, 2023 at 18:02
  • Generally I agree :). Here you would record immediately prior to using, so not an issue. This is still an elegant solution to having to repeat something you find yourself having just executed instinctively without planning for repeats.
    – CrashNeb
    Apr 30, 2023 at 20:50
  • 1
    It works as long as the search part of the substitution match something on the lines but it stop otherwise. May 9, 2023 at 9:18

Your Answer

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

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