Coder Perfect

When using the ‘less’ command in Unix, show special characters.

Problem

I’m curious about how to display special characters when using the ‘less’ command. I’d like to see the non-printable characters with a special indication, for example. For example, I use “set list on” in the vi editor to display the line termination characters represented by the dollar ‘$’ character. Similarly, I’d like to accomplish this using the ‘less’ command.

I tried referring to the Unix less handbook, but it didn’t help.

Asked by Kingsly

Solution #1

If there is a variable named LESS in its environment, less will look for it.

You can put LESS in one of your /.profile files (.bash rc, etc.) and it will discover it whenever you run less from the command line.

Try adding this

export LESS="-CQaix4"

This is the setup I use; there are certain behaviors contained in it that may be confusing, so use the help feature in less to figure out what they all mean. Simply hit the ‘h’ key and poke around, or run less —help.

Edit:

I checked the documentation and saw that there is also a -r option.

-r  -R  ....  --raw-control-chars  --RAW-CONTROL-CHARS
                Output "raw" control characters.

I agree that a cat is the best match for your stated requirements.

cat -vet file | less

Each line will have a ‘$’ at the conclusion and the tab char will be converted to a visual ‘I’.

cat --help
   (edited)
    -e                       equivalent to -vE
    -E, --show-ends          display $ at end of each line
    -t                       equivalent to -vT
    -T, --show-tabs          display TAB characters as ^I
    -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB

I hope this information is useful.

Answered by shellter

Solution #2

That’s something you can perform using cat and then pipe the output to less:

cat -e yourFile | less

This passage from Man Cat explains what the suffix -e denotes:

   -e     equivalent to -vE

   -E, --show-ends
          display $ at end of each line

   -v, --show-nonprinting
          use ^ and M- notation, except for LFD and TAB

Answered by Costi Ciudatu

Solution #3

Use -u to show carriage returns (M) and backspaces (H) for less, or -U to show previous and tabs (I) for example:

$ awk 'BEGIN{print "foo\bbar\tbaz\r\n"}' | less -U 
foo^Hbar^Ibaz^M

(END)

The output without the -U switch would be:

fobar   baz

(END)

For a more detailed description of the features, see man less.

Answered by James Brown

Solution #4

In the same spirit as https://stackoverflow.com/a/6943976/7154924:

cat -A

-A, --show-all
       equivalent to -vET
-v, --show-nonprinting
       use ^ and M- notation, except for LFD and TAB
-E, --show-ends
       display $ at end of each line
-T, --show-tabs
       display TAB characters as ^I

Alternatively, or simultaneously, you can pipe to tr to replace arbitrary characters with the required ones for display before piping to a pager like less.

Answered by flow2k

Solution #5

Notation in less is used to depict all special, nonprintable characters. Line feed, on the other hand, is printable (simply make a new line) and hence not regarded distinctive, so replacing it will be difficult. If all you want to see are line endings, the simplest method might be

sed -e 's/$/$/' | less

Answered by thiton

Post is based on https://stackoverflow.com/questions/6943928/show-special-characters-in-unix-while-using-less-command