Tip 92: Repeat the Previous Substitute Command
Return to the top: <<Practical Vim>>
Repeat a Line-Wise Substitution Across the Entire File
- 
repeat the command across the entire file just by pressing 
g& - 
equivalent to: repeat the last substitute command using the same flags, the same replacement string, and the current search pattern, but use the 
%range.:%s//~/&
 
References:
# If you open the file in vim, # then you can \"ayy@a the next line to execute the vim command: :h g&
Amend the Range of a Substitute Command
- 
gvcommand enables Visual mode and rehighlights the last selection. - 
:&&- 
First 
&forms the:&Ex command, which repeats the last:substitutecommand. 
 - 
First 
 
References:
# If you open the file in vim, # then you can \"ayy@a the next line to execute the vim command: :h :&
- 
Second 
&indicates that the flags from the previous :s command should be reused. 
Discussion
- 
&command is a synonym for:s, which repeats the last substitution. - 
:&&: acts on the current line - 
:'<,'>&&acts on the visual selection - 
:%&&acts on the entire file. - 
g&is a shortcut for:%&& 
Fixing the & Command
preserves flags for the & command:
nnoremap & :&&<CR> xnoremap & :&&<CR>