Different show between “cat” and “vim” [closed]

2019-06-14 18:55发布

cat /tmp/test:

>> cat /tmp/test
Hello World
Hello World
Hello World
Hello World

vim /tmp/test:

>> vim /tmp/test
==>
1 ^[[1;31mHello World^[[0m
2 ^[[2;32mHello World^[[0m
3 ^[[5;33mHello World^[[0m
4 ^[[6;34mHello World^[[0m

Sorry , I have google it , but I still don't know how to let vim /tmp/test show the same with cat /tmp/test.

Could you please give me a hand? thanks.

2条回答
Deceive 欺骗
2楼-- · 2019-06-14 19:18

If you're asking why "garbage" characters are showing up at the beginning and end of each line in Vim but not with cat, the reason is probably that they are formatting escape sequences that the shell uses to color text. Since cat sends its output directly to the shell, the escape sequences are interpreted as formatting commands, producing the colored text. Vim is not designed to use these formatting sequences, so it just displays them as part of the text.

To get rid of the escape characters, you could delete them manually, or you could use a tool like sed to filter them out as outlined here.

查看更多
在下西门庆
3楼-- · 2019-06-14 19:30

To view produced output like it is shown in shell you may want to use AnsiEsc plugin. After you open file with such escape codes you should run :AnsiEsc and it will be highlighted.

查看更多
登录 后发表回答