Chapter 3. Vim Editor

Normal Mode (6) – Replace Phrase

Normal Mode (6) – Replace Phrase
Tag:

In Normal mode, you can search for a phrase and replace it with a new phrase using the command below.

Command Line - INTERACTIVE
:[search area]s/[search phrase]/[replace phrase]/option

Search area

% : you can search and replace a phrase in the entire document
n : you can search and replace a phrase under line n

Option

g : you can replace all phrases in the selected search area. If you select the entire document, option g is the default setting.
c : you can make confirmations for replacement one by one

Examples

Replace the first A with B in line 3 (no confirmation)

Command Line - INTERACTIVE
:3s/A/B

Replace all A with B in line 3 (no confirmation)

Command Line - INTERACTIVE
:3s/A/B/g

Replace all A with B in line 3 (one-by-one confirmation)

Command Line - INTERACTIVE
:3s/A/B/gc

Replace all A with B in the entire document (no confirmation)

Command Line - INTERACTIVE
:%s/A/B/g

Replace all A with B in the entire document (one-by-one confirmation)

Command Line - INTERACTIVE
:%s/A/B/gc

In Normal mode, you can search for a phrase and replace it with a new phrase using the command below.

Command Line - INTERACTIVE
:[search area]s/[search phrase]/[replace phrase]/option

Search area

% : you can search and replace a phrase in the entire document
n : you can search and replace a phrase under line n

Option

g : you can replace all phrases in the selected search area. If you select the entire document, option g is the default setting.
c : you can make confirmations for replacement one by one

Examples

Replace the first A with B in line 3 (no confirmation)

Command Line - INTERACTIVE
:3s/A/B

Replace all A with B in line 3 (no confirmation)

Command Line - INTERACTIVE
:3s/A/B/g

Replace all A with B in line 3 (one-by-one confirmation)

Command Line - INTERACTIVE
:3s/A/B/gc

Replace all A with B in the entire document (no confirmation)

Command Line - INTERACTIVE
:%s/A/B/g

Replace all A with B in the entire document (one-by-one confirmation)

Command Line - INTERACTIVE
:%s/A/B/gc
Tag: