How do I use expressions in Vim's substitute replacement?
:%s/pattern/\=expression/g
Vim's substitute command supports expression replacements using \= in the replacement string.
411 results for "G"
:%s/pattern/\=expression/g
Vim's substitute command supports expression replacements using \= in the replacement string.
:%!tac
Vim doesn't have a built-in reverse command, but you can pipe the buffer through tac (reverse of cat) to flip line order.
:s/,/,\r/g
In Vim's :substitute command, \r in the replacement string inserts a literal newline — it splits the line at that point.
: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/pattern/replace/flags
The substitute command supports several flags that modify its behavior.
:%s/\(pattern1\)\(pattern2\)/\2\1/g
Vim's substitute command supports capture groups that let you match parts of text, remember them, and rearrange or reuse them in the replacement.
:cdo s/old/new/g | update
The :cdo command executes a given command on every entry in the quickfix list.
command-line #command-line #quickfix #batch-editing #search #multi-file
:v/pattern/command
:v (short for :vglobal) is the inverse of :g.
ggVG
While Vim doesn't have a built-in "entire buffer" text object, the ggVG sequence achieves it: go to the first line, enter line-wise visual mode, then select to
:'a,'b s/old/new/g
Marks can be used as range specifiers in any Ex command.
command-line #marks #command-line #ex-commands #ranges #editing
:keeppattern %s/old/new/g
When you run a :s or :%s substitute command, Vim updates the search register (@/) with the substitution pattern.
command-line #ex-commands #search #editing #registers #substitute
:%s/\%Vpattern/replacement/g
The \%V atom restricts a regex match to the last visual selection — more precisely than :'s/.
"/p
Vim stores the last search pattern in the search register "/.
:/start/,/end/d
Instead of specifying line numbers for Ex command ranges, you can use search patterns.
command-line #ex-commands #editing #search #ranges #command-line
:%s/\<word\>/replacement/g
How it works In Vim's regular expressions, \ are word boundary anchors: \ matches the end of a word.
:%s/pattern/\=@a/g
How it works In Vim's substitute command, the replacement string can be a Vimscript expression when prefixed with \=.
autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
Vim remembers the last cursor position for every file you edit (stored in the viminfo or shada file), but by default it opens files at line 1.
42G
The 42G command jumps the cursor directly to line 42 in the current file.
qa:s/old/new/g<CR>jq
How it works You can combine Ex commands like :s (substitute) with macro recording to create powerful repeatable find-and-replace operations that go beyond what
:WhichKey
which-key.