kal
kal

Reputation: 29401

Regular expression to replace first underscore with comma

I have this pattern

a,abc_def_eghi
1,234_556
5,567_987_ghi

I want to replace the first _ with a ,. I know %s/old/new/g to replace contents in Vim.

Result

a,abc,def_eghi
1,234,556
5,567,987_ghi

Could you suggest some alternatives to go about it

Upvotes: 0

Views: 507

Answers (2)

Kevin Beck
Kevin Beck

Reputation: 2390

The g is for "global". If you leave it off, the substitution will apply only once on each line.

%s/old/new/

Upvotes: 8

Stephan202
Stephan202

Reputation: 61549

If you only want to replace the first occurrence of a match, do not use the g modifier. That is, use s/old/new/ instead of s/old/new/g. More vim search/replace tips and tricks can be found over at Wikia.

Upvotes: 3

Related Questions