Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a long string that needs to be displayed with limited text-width, is there a command line tool in *nix that converts the single-line string to a multi-line string with each line being no longer than a given text-width?

For example, given the following string

$ MYSTRING="Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world."

I would like to format somewhat like this:

$ echo $MYSTRING | special-format-command --width=30
Call me Ishmael. Some years ag
o - never mind how long precis
ely - having little or no mone
y in my purse, and nothing par
ticular to interest me on shor
e, I thought I would sail abou
t a little and see the watery 
part of the world.
share|improve this question
up vote 19 down vote accepted

You might try the fold command:

echo "$MYSTRING" | fold -w 30
share|improve this answer
+1, but perhaps better written as echo $MYSTRING | fold -w 30, since the -30 syntax doesn't appear in the fold manpage? – ire_and_curses Jan 3 '13 at 23:14
For posterity, there is also fmt. The difference between fold and fmt is that fold will wrap at the fixed width, even splitting words and may begin lines with blanks. While fmt will wrap near or under the width, it will not split words, lines will not begin with blanks and short lines may be joined. – bahamat Jan 3 '13 at 23:15
fold appears to have similar behavior to fmt when the -s option is used. – Cory Klein Jan 3 '13 at 23:16
@ire_and_curses Thank you! Updated answer. – Christopher Jan 3 '13 at 23:16
funny, i used -w option but it split it by char. – amphibient Jan 4 '13 at 2:26

Here's a method using sed:

sed 's/.\{30\}/&\
share|improve this answer
But what does it mean? – Richard Sep 18 '15 at 6:15
@Richard Replace 30 of any character except newline (.\{30\}), with the same string (&), followed by a newline. – Chris Down Sep 18 '15 at 8:22

The entire purpose of fmt (part of GNU coreutils, and IIRC other versions were included with non-GNU *nixes, so is almost certain to be already installed) is to reformat paragraphs.

However, there is an even better tool called par (for paragraph reformatter). It can do everything that fmt can do, but with a lot more options to control the exact output format.

One of the many really nice things about par is that it is very smart about handling quoting in emails (even multiple levels of quoting) - very useful...I have my .vimrc set up so that hitting F5 sends the current paragraph through par.

If it's not packaged for your distro (it is for debian and derivatives, probably others too), par can be found at http://www.nicemice.net/par/

share|improve this answer
By "non-GNU *nixes", do you mean the "real" (not "Unix-like") Unixes? (Otherwise cool tool, it seems.) – Emanuel Berg Jan 4 '13 at 9:34
i mean now-obsolete proprietary junk *nixes. – cas Jan 4 '13 at 9:35
That's what I thought, but why are you then using the asterisk? I thought that was to get around having to type "Unix-like" all the time (for Linux etc.). (I'm not bugging you over a detail - or well, I am - but I really want to know, I'm not bugging you for the sake of it.) – Emanuel Berg Jan 4 '13 at 10:17
*nix is just a way of saying "generic unix or unix-like systems" - it dates from a time when the word Unix was a proprietary trademark of AT&T – cas Jan 4 '13 at 23:22

fold -s is the shortest, best answer to your question.

However, as good as those one-purpose tools are in some cases, in general, you should have your editor do this.

In Emacs, you could tweak this anyway you'd like. As for me, I use visual-line-mode (shows up as a Wrap minor mode in the mode line) because in the general case, I don't want line breaks anywhere else than at the end of paragraphs.

I thought I'd tell the way I do it. It is not "the" way, but it is one way; as I mention the functions and variables, your way is to put them together and automatize them to make them work for you. (This is Emacs, but for any sufficiently advanced editor, a similar post could be written.)

I said I have the lines visually wrapped when I type, and never break them manually. As always, there are exceptions: for example, GNUS won't send posts to Usenet with lines beyond a certain width (in characters). So, then I use M-q. In Emacs, M-q runs the command fill-paragraph. The variable fill-column controls the width for filling. As an example, if I write this (on a single line)

This is a lot of text being written right now. The purpose is to overflow the line so I have have to line break it with M-q.

and then use M-q, I get

This is a lot of text being written right now. The purpose is to
overflow the line so I have have to line break it with M-q.

With justify-current-line (twice as two lines), you get

This  is a lot  of text  being written  right now.  The purpose  is to
overflow  the  line  so  I  have  have to  line  break  it  with  M-q.

But, I've found that people seldom use monospace fonts to view things, so it didn't make much sense using it (apart for my own viewing pleasure). If you like it, you could tweak fill-paragraph to fill and justify at the same time. You could also have Emacs do this for you, always, as you type: auto-fill-mode. (I found that stressful, but that's me.)

This may seem like a lot of work, and it is; but once you're done (with this and 1000 other things) you can really enjoy working with the tools behaving precisely as you like them to.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.