Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm composing a plain text (.txt) document that has left-and-right justified paragraphs. I manually add spaces, breaking words with hyphens if necessary, until the lines are uniform length. To use an example from my document, a paragraph like this:

These phantasmic balls have some strange properties, some neat, some
interesting, and others just indicative of lazy design. They're almost all
caused by the same design flaw: The game treats them too much like regular
balls.

will end up looking like this:

These phantasmic balls have some  strange properties,
some neat,  some interesting, and others just indica-
tive of lazy design. They're almost all caused by the
same design flaw:  The game treats them too much like
regular balls.

How can I do this?

It's okay to add spaces (without breaking words over lines), but I need to control the target width of the paragraph (in # characters). The example above is justified to 53 characters per line, but line width in my actual document varies from 60 to 79.

It'd also be great if there was a text editor with such a feature.

I already tried:

  • OpenOffice and TeX: They just use formatting to change how the text is displayed.
  • nano's Justify command doesn't count because it doesn't actually make the lines uniform width.

I accepted the following answer because it was the one that worked best for me. I found the emacs fill commands the easiest to use among the answers, and I appreciate how it recognizes even non-whitespace indentation (for stuff like # and // comments). However, the other answers all fill their own niches and I recommend others visiting this question to look at them as well.

share|improve this question
1  
Why? I'm genuinely curious. One of the advantages of plain text it that it's independent of formatting and you can slap it into OpenOffice or TeX and make it look nice. What's your use for this? –  Doug Harris Aug 3 '10 at 21:46
    
Also, are you looking for this utility to do semi-intelligent hyphenation too? –  Doug Harris Aug 3 '10 at 21:46
    
The reason is because it's a guide for GameFAQs. GameFAQs only takes plain text, TXT documents. As for hyphenation, I'd prefer it, but it isn't an absolute requirement. –  Exp HP Aug 3 '10 at 22:02

5 Answers 5

up vote 3 down vote accepted

You can do this in emacs using fill-paragraph or fill-region (fill docs).

You need to pass a numeric prefix argument. The default fill column is 70.

Select the text to fill and do M-3 M-x fill-region.

I set the fill column to 53 for your sample text and got:

These phantasmic balls  have some strange properties,
some   neat,  some   interesting,  and   others  just
indicative of lazy  design. They're almost all caused
by  the same design  flaw: The  game treats  them too
much like regular balls.
share|improve this answer
    
That looks good. I'm installing the package for emacs right now to take a look at this feature. –  Exp HP Aug 3 '10 at 22:09
    
Okay, it took quite a bit of tinkering to get used to the unusual interface and terminology (unusual by today's standards, at least), but this is great. C-x f 53, M-1 M-q. It's certainly easier than my Plan B (which would've been to write my own Ruby script that justified text), and much to my satisfaction, it even recognizes indentation and takes that into account when justifying. Nice. –  Exp HP Aug 3 '10 at 23:19

you need par

e.g. to get it on ubuntu, do:

sudo aptitude install par

to justify text.txt (at 80 chars), saving in newtext.txt:

par 80j < text.txt > newtext.txt

to use par in vim or gvim:

:set formatprg=par\ 80j

then highlight the text you want to format and use the gq command.

for more information, see man par

share|improve this answer
    
I tested this and found it to be quite touchy. It sometimes results in errors even under what would seem to be the most innocent of circumstances. In comparison to the emacs command, this is unusable. –  Exp HP Aug 4 '10 at 22:59
    
Can you give an example? It seems quite stable to me. –  sml Aug 5 '10 at 0:41
1  
The command you gave is not correct. It has to be par w80 j1, not par 80j. Edit: Or par jw80 –  Gandaro May 27 at 19:01

Here's a Perl script called paradj that can do full justification and hyphenation.

Here's a diff showing some changes I made to support adding a left margin:

12c12
< my ($indent, $newline);
---
> my ($indent, $margin, $newline);
15a16
>   "margin:i" => \$margin,
21a23
> $margin = 0 if (!$margin);
149a152
>     print " " x $margin;
187a191,193
>   print "--margin=n (or -m=n or -m n)  Add a left margin of n ";
>   print "spaces\n";
>   print "                                (defaults to 0)\n";

This command:

./paradj.pl -h --width=53 --both --indent=0 inputfile

produces output very close to your example (the extra spaces are in different places).

These  phantasmic balls have some strange properties,
some  neat, some interesting, and others just indica-
tive of lazy design. They're almost all caused by the
same  design flaw: The game treats them too much like
regular balls.
share|improve this answer
    
A great solution that, unfortunately, I cannot use. About a month ago, I tried to get TeX in Perl because I wanted to use TeX::Hyphen (for a different but related issue to this question). IIRC, what I discovered was that the Ubuntu repos only have certain parts of the TeX library available, and Hyphen is not among them. I tried manually installing the Perl TeX library, but that did not work out well. I ended up breaking Perl and spent the next few hours undoing the damage. I'm reluctant to give it another go. I wish I had enough rep to at least give you a +1 for this answer, though. –  Exp HP Aug 3 '10 at 23:56
    
@Exp HP: I'm sorry you had so much trouble. I've got this on a Ubuntu 9.10 system and I've no problems. –  Dennis Williamson Aug 4 '10 at 1:51

fmt is the old-school solution

From the man page:

fmt [-cmnps] [-d chars] [-l num] [-t num] [goal [maximum] | -width | -w width] [file ...]

and

The goal length defaults to 65 and the maximum to 10 more than the goal length. Alterna- tively, a single width parameter can be specified either by prepending a hyphen to it or by using -w. For example, fmt -w 72'',fmt -72'', and ``fmt 72 72'' all produce identical output.

share|improve this answer
    
Some versions of fmt don't have several of those options and can't do full justification. –  Dennis Williamson Aug 8 '10 at 7:02

I use Nano.

Rename the file to a shorter name (8.3). Open in Nano, then Alt-J to justify the whole document. Alt-O to save. Then Alt-X to exit. Then rename the file back to the original name.

Might sound cumbersome but it saved me a lot of time over doing the manual Enter, Delete, End to limit the columns to 90. Nano defaults to 72. I can live with that.

share|improve this answer
    
What is the magic behind the short file name? –  Jawa Aug 28 '13 at 5:50

Your Answer

 
discard

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.