• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Vim Find And Replace

Thumbnail for Glory KimGlory Kim

Glory Kim

Software Engineer, Loom

Updated: 6/25/2024

Published: 12/31/2022

About Terminus

The short answer

In Vim, to search and replace the first occurrence of a string on the current line (i.e. the line the cursor is on), you can use the substitute command (:s) as follows:

:s/<pattern>/<replacement>

Run in Warp

Where:

  • pattern is the string you want to replace.
  • replacement is the string you want to replace the pattern with.

For example, this command will replace the first occurrence of the string "foo" with the string "bar":

:s/foo/bar

Run in Warp

You can also read our other article on how to search for patterns in Vim.

Replacing all the occurrences of a pattern

To replace all the occurrences of a pattern on the current line, you can append the g flag to the substitute command as follows:

:s/<pattern>/<replacement>/g

Run in Warp

Replacing all the occurrences of a pattern in the entire file

To replace all the occurrences of a pattern in the entire file, you can prepend the substitute command with a percentage sign (%) as follows:

:%s/<patter>/<replacement>

Run in Warp

Replacing the occurrences of multiple patterns

To find and replace the occurrences of multiple patterns at once, you can use the following command:

:[%]s/<pattern>[\|<pattern>...]/<replacement>/[g]

Run in Warp

Where:

  • <pattern>[\| <pattern>...] is a list of patterns separated by \|.

For example, this command will replace all the occurrences of the strings "email" and "login" with the string "credentials" in the entire file:

:%s/email\|login/credentials

Run in Warp

Making the substitute command case insensitive

To replace the occurrences of a pattern regardless of its case, you can append the i flag to the substitute command as follows:

:[%]s/<pattern>/<replacement>/[g]i

Run in Warp

Replacing the occurrences of special characters

Replacing tabs with spaces

Using the pattern matching approach above, we can use vim substitute to replace tabs with spaces:

:[%]s/\t/ /[g]

Run in Warp

Replacing double quotes with single quotes

Similarly, to replace double quotes with single quotes on the entire file, developers can run this command:

:[%]s/"/'/[g]

Run in Warp

Limiting the pattern substitution to specific lines

In Vim, there are several ways to limit the substitution of patterns to a specific number of lines.

Substituting between a line range

To replace a pattern between specific lines, you can use the substitute command as follows:

:<start>,<end>s/<pattern>/<replacement>/g

Run in Warp

Where start and end are the line numbers the substitution should happen between.

For example, this command applies the substitution between the lines 2 and 7:

:2,7s/hello/world

Run in Warp

Easily retrieve this syntax using Warp's Agent Mode:

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp Agent Mode feature:

Thumbnail for

EnteringWhat is the Vim command to substitute a pattern in a line range? in the Agent Mode question input will prompt a human-readable step by step guide including code snippets.

Substituting N lines from the current line

To replace a pattern starting from the current line to a specific line number, you can use the substitute command as follows:

:.,+<lines>s/<pattern>/<replacement>/g

Run in Warp

Where:

  • . represents the current line.
  • lines represents the amount of lines.

For example, this command applies the substitution from the current line on to the next 7 lines:

:.,+7s/hello/world

Run in Warp

Substituting from the current line to the end of the file

To replace a pattern starting from the current line to the end of the file, you can use the substitute command as follows:

:.,$s/<pattern>/<replacement>/g

Run in Warp

For example, this command applies the substitution from the current line to the end of the file:

:.,$s/hello/world

Run in Warp

Substituting in a visual block selection

To replace a pattern with a visual block selection, you can use the substitute command as follows:

:'<,'>s/<pattern>/<replacement>/g

Run in Warp

Where:

  • '< represents the start of the visual block.
  • '> represents the end of the visual block.

Deleting the occurrences of a pattern

To delete all the occurrences of a pattern, you can leave the replacement string blank as follows:

:[%]s/<pattern>//[g]

Run in Warp

Written by

Thumbnail for Glory KimGlory Kim

Glory Kim

Software Engineer, Loom

Filed Under

Related Articles

Vim Modes

Learn about seven of Vim’s modes

VimVi
Thumbnail for Razvan LudosanuRazvan Ludosanu

Vim / Vi Page Up and Down Controls

Quick reference for Vim's page up and down controls

VimVi
Thumbnail for Glory KimGlory Kim

Copy & Paste in Vim / Vi

Copy (Yank), Paste (Put) and Cut (Delete) in Vim

VimVi
Thumbnail for Jessica WangJessica Wang

Undo & Redo in Vim / Vi

Keyboard shortcuts and summary of how Vim tracks changes

VimVi
Thumbnail for Jessica WangJessica Wang

Show & Hide Line Numbers in Vim / Vi

Toggle absolute and relative line numbers

VimVi
Thumbnail for Amber ChenAmber Chen

Select all in Vim / Vi

Select all and copy, paste, or delete the contents

VimVi
Thumbnail for Glory KimGlory Kim

Searching in Vim

Search forward, backward, case insensitive, and more

VimVi
Thumbnail for Neeran GulNeeran Gul

Go To End of File in Vim / Vi

Select and delete to the end of a file in Vim

VimVi
Thumbnail for Preston Tunnell WilsonPreston Tunnell Wilson

How to delete lines in Vim / Vi

The best answer depends on which mode you use

VimVi
Thumbnail for Andrew CarlsonAndrew Carlson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null