Purboo
Purboo

Reputation: 101

How to substitute multiple characters including beginning of line and end of line in vim?

I would like to add '|' at the beginning of line, end of line, and replace whitespace with '|'. For example,

123 456 789
abc def hij

should become

|123|456|789|
|abc|def|hij|

The command 1,$s/[ $^]/|/g only replaces the whitespace but not the ^ (beginning of line) and $ (end of line).

What is the right regex to achieve my goal?

Upvotes: 0

Views: 74

Answers (1)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

The regex to be used will be,

"^|$| "

Which when have to be written for VIM editor becomes this as you need to escape | with \,

:%s/^\|$\| /\|/g

Open your file, then just copy paste above string and hit enter, which will give you desired results.

Upvotes: 2

Related Questions