Skip to content

Substitute commandλ︎

:substitute or :s command highlights the matches for a text pattern and substitute for a new pattern

Neovim :help :substitute
:help :substitute

Subsitute the first matching patterns in the current line


If the new-pattern text is ommitted, then substitute deletes the current-pattern occurances, e.g :s/current-pattern//

Subsitute all the matching patterns in the current line, g representing all occurances in a line


Use % to specify the current buffer as the scope to change all matches


An inclusive line range can be specified to narrow the search


. can be used to represent the current line of buffer

$ to represent the last line of the current buffer


Match the whole word


Substitute historyλ︎

:s and the Up / Down will navigate through the substitution history for the current session (from when Neovim was last opened if session was not restored)

Confirm replacementλ︎

c option at the end prompt for confirmation to replace each occurance


y confirms the repacement

l confirms the repacement and quits

n skips the current occurance and goes to the next one

y or Esc to quit substitution

Regular expressionλ︎

regular expressions can be used as a search pattern.

To replace all lines starting with ‘foo’ with ‘NeoVim Rocks’:

:%s/^foo.*/NeoVim rocks/gc

Replace all instances of ‘apple’, ‘orange’, and ‘mango’ with ‘fruit’:


Remove trailing blank space at the end of each line:


Matching caseλ︎

i option disables the default case sensitive search


Visual Selectλ︎

Use a visual select to search and replace, with confirmation

Note: '<,'> is automatically included when in visual mode and : is pressed to start a command


A potentially more effecitve approach:

  • visually select the text
  • * to select all matching occurances
  • :%s//replace-text/g