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

ls -1 > textfile

Output looks fine with more.


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


Can anyone explain what is going on here?

  • look like a end of line issue – Kiwy Feb 13 '14 at 10:07

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:

          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:

          Like -r, but only ANSI "color" escape sequences are output in "raw" form.

Try less textfile vs. less -R textfile.

| 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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