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.
Why is it and how can I redirect the man page to a file correctly.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityNotice 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
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
.
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 usingman
normally). As for storing just the plain text,col -bx
is the usual filter (see this answer).