0

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.

iceKing
  • 147
  • 3
  • 13

2 Answers2

4

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.

blm768
  • 310
  • 1
  • 3
  • 12
  • Thanks for your response. If I have some messages show on console with color, and redirect to a file, then is it the only method to show the messages with original format by "cat file"? – iceKing Aug 29 '13 at 02:54
  • That's the only method that comes to mind if you're viewing the file in a terminal, but the programs mentioned in [this question](http://stackoverflow.com/questions/2033268/linux-shell-output-to-html) can convert the file to HTML, RTF, or some other standard format that supports colored text. – blm768 Aug 29 '13 at 04:29
  • If you're just using vim to view the output of another command, might I be so bold as to suggest using `less -R`? This will use less as a pager, allowing you to capture the output and page back/forwards and, with the `-R` flag, will display ANSI colour sequences (the extra characters you are seeing) – sanmiguel Aug 29 '13 at 15:21
0

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.

ZyX
  • 52,536
  • 7
  • 114
  • 135