How do I search and replace text only on the current line?
:s/old/new/g
The :s/old/new/g command replaces all occurrences of old with new on the current line only.
139 results for "S substitute line"
:s/old/new/g
The :s/old/new/g command replaces all occurrences of old with new on the current line only.
getreg()
The getreg({name}) function returns the content of any register as a string.
:/pattern/+N and :/pattern/-N
Vim's Ex command ranges can use search patterns as line addresses, and those addresses can include a numeric offset (+N or -N) to target lines relative to the m
:help registers
Vim has 10 types of registers, each serving a specific purpose.
<C-\>e
Pressing e on the command line opens a special prompt that lets you type a Vimscript expression.
command-line #command-line #ex-commands #vimscript #insert-mode
:g/pattern1/s/pattern2/replacement/g
Combining :g with :s lets you apply a substitution using two independent patterns: :g selects which lines to act on, and :s controls what gets replaced within t
:g/outer/,/end/g/inner/
The :global command accepts a range, which lets you scope its search to sections of the file rather than the entire buffer.
:argdo %s/old/new/g | w
:argdo {cmd} executes an Ex command against every file in the argument list—the set of files you opened Vim with or set explicitly with :args.
:cdo {cmd}
:cdo executes an Ex command on every entry in the quickfix list in sequence, visiting each match in turn.
q: or <C-f> from : prompt
The command-line window (q:) opens a full Vim buffer containing your Ex command history.
/\%Vpattern
The \%V atom restricts a search pattern to only match inside the most recent visual selection.
<C-f> (from command-line mode)
When you are partway through typing a long or complex Ex command on the : prompt, you can press to open the command-line window.
<C-r><C-r>
In command-line mode, {reg} inserts a register's contents — but it processes certain sequences, potentially misinterpreting backslashes, pipe characters, or e
g*
The g command searches forward for the text under the cursor without adding word boundary anchors.
search #search #navigation #normal-mode #motions #productivity
autocmd BufWritePre * :%s/\s\+$//e
By adding an autocmd for the BufWritePre event, you can make Vim automatically strip trailing whitespace from every line each time you save.
/pattern/+3
Vim's search command accepts an offset after the pattern that shifts where the cursor lands relative to the match.
:args **/*.js | argdo %s/old/new/ge | update
The argument list (arglist) is Vim's mechanism for loading a set of files and running commands across all of them.
command-line #command-line #ex-commands #arglist #productivity #batch #editing
:{range}command
Every Ex command in Vim can be preceded by a range that specifies which lines it should operate on.
command-line #command-line #ex-commands #ranges #editing #productivity
s{char}{char}
The vim-sneak plugin by Justin Keyes provides a motion that lets you jump to any visible location by typing just two characters.