Insights Technical Hub How to search and replace in vi

How to search and replace in vi

Introduction:

vim is an advanced editor that was often used in non-graphical OS environments. VI Improved (vim) is a later file editor built upon VI. vim can be installed on Windows distributions. vim comes with features that make non-graphical file editing efficient.

Requirements:

vi or vim

Procedure:

On Windows, set your computer environment path to the file with vim.exevi should be included in the path on Linux.

On Windows, start the command line by pressing the start(windows key) and r at the same time “start + r” on your keyboard.

Type vim on Windows or vi on Linux into the command line window.

There are few basic editor commands you should know before continuing.
Change to insert mode by pressing i
Change to normal mode by pressing ESC

Searching for text? The / command searches through the file top to bottom, and then wraps from the end to the beginning.

The ? command searches through the file in the reverse direction going from bottom to top, and then wraps from the top (beginning) back to the bottom of the file (end).

Repeat the previous search using the n command.
Repeat the previous search in the opposite direction using the N also called SHIFT-N command.

Replace the first occurrence of “OLD” found on the current line with “NEW” using

:s/OLD/NEW>

Replace all occurrences of “OLD” on current line with “NEW” using /g, for example

:s/OLD/NEW/g

Replace between two lines including those lines using :#,#s/, for example

:#,#s/OLD/NEW/g

Replace every occurrence of “OLD” with “NEW” within the file using :%s, for example

:%s/OLD/NEW/g

Take a look at some examples of the commands.

Data in file before editing the file.
abcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Format of the examples.
Notes about the example. Where was the cursor?
Command syntax example.
Results Found using the command. The bold text are the results found from using the command. Red text is the data in the file.

Cursor was at the beginning of the file.
/abc
abcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Cursor was at the beginning of the file.
?abc
abcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Cursor was at the beginning of the file.
Press ‘n’ to repeat the previous search.
n
abcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Press ‘N’, which is SHIFT-N to search in the opposite direction of the previous search command you inserted.
N
abcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Cursor was at beginning of the file.
Data before replacement.
abcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc
:s/a/z

Data after replacement.
zbcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Cursor at beginning of file.
Data before replacement.
zbcabcabcabc
abcabcabcabc
abcabcabcabc
abcabcabcabc
:s/a/z/g

Data after replacement.
zbczbczbczbc
abcabcabcabc
abcabcabcabc
abcabcabcabc

Data before replacement.
zbczbczbczbc
abcabcabcabc
abcabcabcabc
abcabcabcabc
:2,3s/b/y/g

Data after replacement.
zbczbczbczbc
aycaycaycayc
aycaycaycayc
abcabcabcabc

Data before replacement.
zbczbczbczbc
aycaycaycayc
aycaycaycayc
abcabcabcabc
:%s/[a-zA-Z]b/ /g

Data after replacement
c c c c
aycaycaycayc
aycaycaycayc
c c c c

Let's make your job easier

Sign up to get insightful content