5

I run:

$ man cd > mancd
$ cat mancd

This shows as expected. But when I open the file with VSCode and vim, they are completely different.

enter image description here

enter image description here

Why is it and how can I redirect the man page to a file correctly.

2
  • 6
    The output of man contains formatting trickery, like making characters bold by printing them, backspacing, and printing them again (this used to work directly on old printing terminals). When you view the output in an editor, you see the raw sequences instead of their intended effect (like you see when using man normally). As for storing just the plain text, col -bx is the usual filter (see this answer). Mar 12 at 21:27
  • @GordonDavisson This comment would make a pretty good answer
    – gronostaj
    Mar 13 at 15:28
6

Notice a lot of ^H in the screenshot above. It means Ctrl+H which produces the ASCII 08 character A.K.A. Backspace. When printing a character, move the cursor back with Backspace then overwrite the same character again then it appears darker. That's how bold text is achieved in a mechanical typewriter. Various modern electronic terminals also support that and many CLI tools do use it for text formatting. ANSI sequences beginning with Escape (ASCII 27) are also commonly used for changing cursor position and text formatting properties like blinking, color, italic... Those 08 or 27 characters are called control characters.

cat doesn't know anything about those bytes. It just passes the raw byte stream to the terminal or to the next item in the pipe. Since the terminal knows about those control characters, it'll show the text properly. However vi or VS code isn't a terminal and translates the control characters before showing them inside their windows so that the accidental control characters don't mess up their screen and the terminal

To disable the control characters and just output the plain text then check the options of the tools you use. For man notice the Controlling formatted output section in the man page. Try something like man --ascii cd

However most programs will automatically determine type of the output to know whether it should output the control characters or not, for example most GNU tools use the --color option like ls or grep. So does man:

  • MAN_KEEP_FORMATTING

    Normally, when output is not being directed to a terminal (such as to a file or a pipe), formatting characters are discarded to make it easier to read the result without special tools. However, if $MAN_KEEP_FORMATTING is set to any non-empty value, these formatting characters are retained. This may be useful for wrappers around man that can interpret formatting characters.

So it looks like your man is different or some formatting option has been specified by the alias or environment variables so it outputs the formatting even when you redirect the output to text file

See

1
  • 2
    You will notice that man cd --ascii is not doing what you think it does. However, man --ascii cd might do that (depending on whether you have a cd manual or not). The more portable way of generating plain ASCII output man, would be using -Tascii, and then passing it through col -b.
    – Kusalananda
    Mar 13 at 8:18

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.