Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put words, separated by commas, in quotes

Tags:

vim

vi

I have a long line of words, looking like this:

foo,foo bar,bar@foo,foo# bar,bar$ foo#

Now I would like to turn it into:

"foo","foo bar","bar@foo","foo# bar","bar$ foo#"

Thus, the delimiter is a comma. What is the best way to do this in vi(m)?

EDIT: Would anyone care to elaborate on the downvotes?

like image 651
cherrun Avatar asked Oct 16 '25 10:10

cherrun


1 Answers

This seems to work. Capture everything thats not a comma and replace it with the captured part in quotes. The g at the end of the command says replace all instances that match the regex on the line. Without the g it would only match the first one. Take a look at :h :s and:h :s_flags

:%s/\([^,]\+\)/"\1"/g

Input:

foo,foo bar,bar@foo,foo# bar,bar$ foo#

Output:

"foo","foo bar","bar@foo","foo# bar","bar$ foo#"
like image 150
FDinoff Avatar answered Oct 19 '25 00:10

FDinoff