How do I refer to the matched text in a Vim substitution replacement?
:%s/\w\+/(&)/g
In a Vim substitution, & in the replacement string expands to the entire matched text.
795 results for "g* g#"
:%s/\w\+/(&)/g
In a Vim substitution, & in the replacement string expands to the entire matched text.
<C-w>g<C-]>
Pressing g jumps to the definition of the tag under the cursor — just like — but opens the destination in a new horizontal split window.
:%s/\r//g
When a file created on Windows is opened in Vim on a Unix system, lines may retain \r (carriage return) characters, displayed as ^M at the end of each line.
:%s/\w\+/\u&/g
Vim's substitute command supports case-conversion modifiers in the replacement string.
:'<,'>s/\n/, /g
Vim's J command joins lines with a single space, but sometimes you need a custom separator like a comma, pipe, or semicolon.
editing #editing #ex-commands #visual-mode #substitution #lines
:set operatorfunc=MyFunc<CR>g@
Vim lets you define custom operators that behave like built-in ones (d, c, y) — they wait for a motion or text object, then act on the selected region.
:s/pattern/\=expression/g
Prefixing the replacement string with \= in a :substitute command tells Vim to evaluate the rest as a VimScript expression rather than literal text.
: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
:g/pattern/t.
Combining :global with the :t (copy) command and the .
:%s/,/,\r/g
In Vim substitutions, \n and \r behave differently depending on whether they appear in the search pattern or the replacement string — a common gotcha that sur
:g/pattern/z#.5
The :global command is great for finding lines matching a pattern, but by default it only shows the matching lines themselves.
g<C-a> in visual mode
When you have multiple lines with the same number and want to turn them into a sequence (1, 2, 3.
:execute "normal! gg=G"<CR>
:execute lets you build and run Ex commands dynamically, which is critical when a command depends on variables, conditionals, or string composition.
command-line #command-line #ex-commands #normal-mode #automation
:set operatorfunc=MyFunc<CR>g@{motion}
Vim's operatorfunc and g@ let you define custom operators that accept any motion or text object, just like built-in operators d, c, and y.
<C-r>=substitute(getreg('+'), '\n\+', ', ', 'g')<CR>
When you paste from the system clipboard into code or config, multiline text often needs to be flattened first.
registers #registers #insert-mode #expression-register #text-processing
:g/^/m 0
This clever use of the :global command reverses every line in the current buffer.
command-line #editing #ex-commands #global #text-manipulation
:%s/pattern/\=@0/g
The \=@0 replacement expression inserts the contents of register 0 (last yank) as the replacement text.
v{motion}<C-g>
Vim has a lesser-known select mode that behaves like selection in typical GUI editors: any typed character replaces the selection.
:g/pattern/+1d
Using :g/pattern/+1d you can delete the line that comes right after each line matching a pattern, in one pass.
:g/pattern/normal dd
The :g/pattern/normal {commands} command executes normal mode keystrokes on every line in the file that matches the given pattern.