Skip to content

fold command in MacOS

The MacOS fold command is a helpful utility for wrapping lines of text to a specific width. It assists in organizing and formatting text files with ease.

fold Syntax:

Terminal window
fold [options] [filename]

MacOS fold Options:

OptionDescription
-sUse spaces instead of tabs for padding
-wSpecifies the width for each line

fold Parameters:

ParameterDescription
filenameThe name of the file to be folded

How to use fold command:

Fold text to fit 80 columns

Terminal window
fold -w 80 file.txt

Folds the text in “file.txt” to fit within 80 columns.

Display a specific number of columns

Terminal window
fold -w 50 file.txt

Displays the text in “file.txt” with a maximum of 50 columns per line.

Use a custom delimiter to fold text

Terminal window
fold -s -w 60 file.txt

Folds the text in “file.txt” to fit within 60 columns, splitting lines at spaces.

Break lines at spaces rather than at columns

Terminal window
fold -s -w 40 file.txt

Folds the text in “file.txt” to fit within 40 columns, only breaking lines at spaces.

Display and pad each line with additional spaces

Terminal window
fold -p -w 70 file.txt

Pads each line of the text in “file.txt” with spaces to fit within 70 columns.

Wrap lines preserving word boundaries

Terminal window
fold -sw 60 file.txt

Wraps the text in “file.txt” to fit within 60 columns, preserving word boundaries.

Fold text only after commas

Terminal window
cat file.txt | tr ',' '\n' | fold -sw 80

Folds the text in “file.txt” only after commas, wrapping lines to fit within 80 columns.

Fold and display text with a specific margin

Terminal window
fold -s -w 60 -m 5 file.txt

Folds the text in “file.txt” to fit within 60 columns with a left margin of 5 characters.

How do I use fold in MacOS?

To use the fold command in MacOS, execute the following command:

Terminal window
fold --width 80 textfile.txt

What does the fold command in MacOS do?

The fold command in MacOS is used to wrap each input line to fit a specified width. It can be useful for formatting text files or controlling the line length.

Terminal window
fold -w 50 textfile.txt

How can I adjust the column width with fold in MacOS?

You can adjust the column width by specifying the desired width in characters when using the fold command in MacOS.

Terminal window
fold -w 100 textfile.txt

Can I use fold to break lines in a text file in MacOS?

Yes, you can use the fold command in MacOS to break lines in a text file by specifying the desired width for line wrapping.

Terminal window
fold -w 70 textfile.txt

How can I display non-printing characters with fold in MacOS?

To display non-printing characters in the output when using fold in MacOS, you can use the -b or —bytes option.

Terminal window
fold -b -w 60 textfile.txt

Is it possible to ignore spaces when using fold in MacOS?

Yes, you can ignore leading spaces when using the fold command in MacOS by using the -s or —spaces option.

Terminal window
fold -s -w 80 textfile.txt

How do I number the lines when using fold in MacOS?

To number the lines when using the fold command in MacOS, you can pipe the output of fold to the nl command.

Terminal window
fold -w 90 textfile.txt | nl

How can I preserve blank lines with fold in MacOS?

To preserve blank lines when using the fold command in MacOS, you can use the -s or —spaces option.

Terminal window
fold -s -w 75 textfile.txt

Applications of the fold command

  • Wrapping lines in a text file to a specific width
  • Forcing the text to break at a certain column width
  • Making text more readable and organized
  • Preparing text for further processing or analysis