Javatpoint Logo
Javatpoint Logo

vi Replace All

In command mode, you can replace a string with another string. For this, first you have to type : to move to ex mode. Then select a range of lines from where strings have to be replaced.

If you want to replace a string starting from first line to the last line 1,$ can be uesd. To replace from a particular line mention the starting and last line number. For example, 2,5 will replace from second line to the fifth line.

Syntax:

:<startLine,endLine> s/<oldString>\/<newString>/g

Note: Here g stands for globally. If the string is repeating more than once in a single line, then g is to be used to replace it globally from all the places in the mentioned line range.

Example 1:

:1,$ s/readable/changed/

Linux Vi Replace All1

Look at the above snapshot, we have written the command :1,$ s/readable/changed/

Linux Vi Replace All2

After pressing enter key, string readable is replaced with string changed.

Example 1:

:3,6 s/letters/neww/g

Linux Vi Replace All3

Look at the above snapshot, we have written the command :1,$ s/letters/neww/g

Linux Vi Replace All14

After pressing enter key, string letters is replaced with string neww.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA