Wrap text files on the command-line for easy reading

fold -s <filename>
fold wraps text at 80 characters wide, and with the -s flag, only causes wrapping to occur between words rather than through them.

9
By: neutral
2009-02-26 05:06:51

These Might Interest You

  • Personally I think line wrap in default df command is annoying for scripting & seeing. So I overwrite it. Maybe more work should be done if wrapped line is over 2... Show Sample Output


    -4
    alias df="df | awk 'NF == 1 {printf(\$1); next}; {print}'"
    MeaCulpa · 2011-04-09 06:43:51 2
  • I used this fragment with Imagemagick convert so that I can place long text strings in pictures. The "\n" gets converted to a true newline in the image. So this fragment uses fold command to wrap the line and then sed to convert newlines (and any trailing spaces on the line) to the text "\n" Show Sample Output


    1
    fold -sw 20 <(echo "Long Text to be wrapped with \"\n\"") |sed ':a;N;$!ba;s/ *\n/\\n/g'
    alecthegeek · 2015-04-16 21:06:53 3
  • wraps text lines at the specified width (90 here). -s option is to force to wrap on blank characters -b count bytes instead of columns


    12
    fold -s -w 90 file.txt
    vincentp · 2009-05-11 23:00:25 1
  • This is assuming that you're editing some file that has not been wrapped at 80 columns, and you want it to be wrapped. While in Vim, enter ex mode, and set the textwidth to 80 columns: :set textwidth=80 Then, press: gg to get to the top of the file, and: gqG to wrap every line from the top to the bottom of the file at 80 characters. Of course, this will lose any indentation blocks you've setup if typing up some source code, or doing type setting. You can make modifications to this command as needed, as 'gq' is the formatting command you want, then you could send the formatting to a specific line in the file, rather than to the end of the file. gq49G Will apply the format from your current cursor location to the 49th row. And so on.


    2
    gqG
    atoponce · 2010-11-08 04:05:24 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: