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