lsund
lsund

Reputation: 744

Split long strings without spaces to multiple lines at a given length in a text file

I have a text file containing some very long lines with no spaces. These lines contain no whitespace or other common delimiters. I would like to split these long lines into seperate lines so that no line in the text file is longer than 80 characters. Is this possible inside vim or perhaps using some other tool?

Upvotes: 0

Views: 344

Answers (1)

SBI
SBI

Reputation: 2322

Quick vim solution:

assume we have a long line of text:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

that we want to split into pieces of 10:

gg (for first line), 
q[letter] (for recording a macro), 
10l (for going right n times), 
a return ESC (for entering a linebreak after current character and leaving insert mode), 
q (to stop recording).

We should now be on line 2 with our cursor, looking like this:

aaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

A quick 100@[letter] formats the entire line:

aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaa

Upvotes: 1

Related Questions