Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to list the components of the current working directory in a text file

ls -1 > textfile

Output looks fine with more.

1010661085645
1010729039145
1010747080245
1010849051345
1010859053445
1011046075845

However when I view this textfile with emacs, several strange characters appear

1010661085645
1010729039145
1010747080245[0m
1010849051345
1010859053445[0m
1011046075845[0m

Can anyone explain what is going on here?

share|improve this question
    
look like a end of line issue –  Kiwy Feb 13 at 10:07

2 Answers 2

up vote 6 down vote accepted

Those "strange characters" are escape sequences for coloring the output.

This will print that number in blue color:

echo -e '\033[01;34m1010729039145\033[0m'

See man console_codes for more details.


You can tell ls in which cases it shall output colors:

   --color[=WHEN]
          colorize the output; WHEN can be 'never', 'auto', or 'always' (the default); more info below

Looks like your ls is actually an alias for ls --color=always. (Type type ls to verify.)

Usually ls --color=auto will do the right thing: It will print colors on screen but no into files.


If you really want the color in your file, your have to decide, if you want to see the actual escape sequences or the interpreted colors.

For example the less command will default to printing the actual sequences, but you can tell it to show the colors instead with the -R option:

   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI "color" escape sequences are output in "raw" form.

Try less textfile vs. less -R textfile.

share|improve this answer

Maybe you have some misconfigurations in emacs, try this:

M-x ansi-color-for-comint-mode-on

Or add this line to your .emacs:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

You can see this for more details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.