Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cat file with no line wrap

Tags:

linux

shell

unix

In *nix, how do I display (cat) a file with no line-wrapping: longer lines should be cut such that they fit into screen's width.

like image 234
Alexandru Avatar asked Oct 23 '09 23:10

Alexandru


People also ask

Which flag turns off line wrapping in less?

According to "less" viewer help, it is disabled with -S or --chop-long-lines flags.

What is the difference between cat file and cat file?

There is no difference from a user point of view. These commands do the same thing. Technically the difference is in what program opens the file: the cat program or the shell that runs it.

What is line wrap mode?

Modes such as Long Lines wrap lines on word boundaries as you type, by inserting temporary line-ending characters. These are removed when you save the buffer to a file or copy its text for yanking elsewhere, so they are only for display purposes.


2 Answers

You may be looking for fmt:

fmt file 

This pretty aggressively reformats your text, so it may do more than what you want.

Alternatively, the cut command can cut text to a specific column width, discarding text beyond the right margin:

cat file | cut -c1-80 

Another handy option is the less -S command, which displays a file in a full screen window with left/right scrolling for long lines:

less -S file 
like image 155
Greg Hewgill Avatar answered Sep 29 '22 03:09

Greg Hewgill


Note that cut accepts a filename as an argument.

This seems to work for me:

watch 'bash -c "cut -c -$COLUMNS file"' 

For testing, I added a right margin:

watch 'bash -c "cut -c -$(($COLUMNS-10)) file"' 

When I resized my terminal, the truncation was updated to match.

like image 30
Dennis Williamson Avatar answered Sep 29 '22 04:09

Dennis Williamson