How can I justify plain text in Linux?
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.
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.
you need par
(official website, source code, package status on Repology)
e.g. to get it on ubuntu, do:
sudo aptitude install par
to justify text.txt
(at the width of 80 chars), saving in newtext.txt
:
par j1w80 < text.txt > newtext.txt
to use par in vim or gvim:
:set formatprg=par\ j1w80
then highlight the text you want to format and use the gq
command.
for more information, see man par