matt
matt

Reputation: 2039

VIM split line into multiple line based on delimiter

In vim, is there an efficient way to split a line such as

a, b, c, d, e

into

a;
b;
c;
d;
e;

Upvotes: 2

Views: 387

Answers (2)

phd
phd

Reputation: 94417

Use command :substitute (search and replace) in a line, replacing globally (all occurrences in the line) , with ;\r (end-of-line):

:s/, /;\r/g

There is no , after the last e so there is no ; after it. Append it manually.

Upd from @SergioAraujo from comments:

:s/, \|$/;\r/g

to search and replace either , or the end of the line.

Upvotes: 4

romainl
romainl

Reputation: 196496

While more expensive in terms of keystrokes, the following method may be more intuitive and require a bit less thinking/planning, which is something to take into account when talking about efficiency:

f,
cW;<CR><Esc>
;.
;.
;.
A;<Esc>

Or, using :help gn:

/,<CR>
cgn;<CR><Esc>
.
.
.
A;<Esc>

Upvotes: 2

Related Questions