Jirr Sawaf
Jirr Sawaf

Reputation: 143

How to add text before the first occurence of a character in Vim?

I have the text

af_ZA_work_013_A;135.300;150.203;Spreker-A;;;[no-speech] #mm
af_ZA_work_013_A;135.300;150.207;Spreker-B;;;[no-speech] #something

I want to add .wav before the first ; in each line, so I would get

af_ZA_work_013_A.wav;135.300;150.203;Spreker-A;;;[no-speech] #mm
af_ZA_work_013_A.wav;135.300;150.207;Spreker-B;;;[no-speech] #something

How can I do this?

Upvotes: 2

Views: 593

Answers (1)

jeremysprofile
jeremysprofile

Reputation: 11425

s/search_regex/replace_regex/ will linewise execute your find and replace.

By default, this is done only on the current line, and only on the first match of search_regex on the current line.

Prepending % (%s/search/replace/) will execute your find and replace on all lines in the file, doing at most one replacement per line. You can give ranges (1,3s will execute on lines 1-3) or other line modifiers, but this isn't relevant here.

Appending g (s/search/replace/g) will do multiple replaces per line. Again, not relevant here, but useful for other scenarios.

You can search for ; and replace with .wav; (there are ways to keep the search term and add to it using capture groups but for one static character it's faster to just retype it).

TL;DR: :%s/;/.wav;/ does what you want.

Upvotes: 1

Related Questions