How do you use a register value in a substitute command?
:%s/<C-r>a/replacement/g
In command-line mode, a inserts the contents of register a.
139 results for "S substitute line"
:%s/<C-r>a/replacement/g
In command-line mode, a inserts the contents of register a.
:%s/\<\w\+\>/\=toupper(submatch(0))/g
The \= flag in the replacement part of :substitute tells Vim to evaluate what follows as a Vimscript expression instead of treating it as a literal string.
:let @q = substitute(@q, 'from', 'to', 'g')
Macros are stored as plain text in named registers, so you can manipulate them with any Vimscript string function.
:s/\v(\w+)\s+(\w+)/\2 \1/
By using capture groups in a substitute command with very magic mode (\v), you can swap two adjacent words in a single operation.
:&&
After running a :s/pattern/replacement/g command, you often need to repeat it on another line or range.
:%s/,/\r/g
In Vim's substitute command, \r in the replacement string inserts a newline.
:s/\(\w\+\)/\u\1/g
Vim's substitute command supports case-change modifiers in the replacement string that let you capitalize, uppercase, or lowercase matched text as part of the s
:cfdo %s/old/new/ge | update
When you grep across your project and want to perform a search-and-replace on every file that matched, :cfdo is the most efficient approach.
command-line #quickfix #substitute #search #ex-commands #editing
:s/pattern/replace/flags
The substitute command supports several flags that modify its behavior.
:%s/\v(\d+)/\=printf('%04d', submatch(1))/g<CR>
When you need to normalize IDs, ticket numbers, or sequence values, :substitute can do more than plain text replacement.
command-line #command-line #substitute #regex #formatting #ex-commands
:%s/^\(.\+\)\n\1$/\1/
This substitute command detects pairs of identical adjacent lines and collapses them into one, using a back-reference to match the repeated content.
\%V (in search/substitute pattern)
The \%V atom in a Vim pattern matches only inside the last visual selection.
search #search #visual-mode #substitution #regex #normal-mode
:'<,'>s/\%Vpattern/replacement/g
Using \%V in a substitute pattern restricts matching to within the visual block area only, rather than the full lines.
:set inccommand=nosplit
When you are crafting a risky :substitute command, the expensive part is usually confidence, not typing.
:keeppatterns %s/old/new/g
The :keeppatterns modifier runs any Ex command without modifying Vim's last search pattern (stored in @/).
command-line #search #ex-commands #command-line #substitute #registers
:s/\%Vfoo/bar/
The \%V atom in Vim's regex engine matches only within the area of the last visual selection.
search #search #visual-mode #substitute #advanced #ex-commands
:%s/pattern/\=@0/g
The \=@0 replacement expression inserts the contents of register 0 (last yank) as the replacement text.
:call setreg("a", substitute(getreg("a"), "old", "new", "g"))
The getreg() and setreg() functions let you read and write register contents as plain strings, making it possible to surgically edit a macro without re-recordin
:s/\v(\S+)\s*=\s*(.*)/\=printf('%-20s = %s', submatch(1), submatch(2))/
When a line contains uneven key = value spacing, quick manual fixes are easy to get wrong.
:%s/^/\=line('.').' '/
Vim's substitute command supports expressions in the replacement string using \=.