Vim Text Coloring

I use echo -e to color output my scripts, it works fine when using standard output.

But when I redirect the output to a file, vim does not display colors; instead, it displays the following characters: [[

example:

 echo -e "\e[32m Green message\e[0m"; echo -e "\e[31m Red message\e[0m"; 

with vim:

 ^[[32m Green message^[[0m ^[[31m Red message^[[0m 

How to fix it.

NB : cat also shows colors !!

+7
vim echo color-scheme
source share
3 answers

Use AnsiEsc Plugin from Dr. Chip or my Colorizer plugin. Both should be able to colorize these terminal exit sequences.

+5
source share

It is generally a bad idea to add any ANSI escape characters to your output if that output is intended to be processed by another program.

Since this is a text editor, it is reasonable to expect Vim to display the contents of the file as is, with escape characters, and not how you want it to look.

So no, nothing can be fixed here. In any case, on the side of Vim.

+2
source share

Redirecting to a file creates this file with all input characters - including color escaping. This is actually the correct behavior, and vim shows you the right things when it shows you these special characters.

It seems like you are probably looking for syntax highlighting. Vim has the ability to understand and color many different types of text.

Try setting the filetype parameter filetype for any type of script that you are using. Once you receive it, you can force vim to automatically install it using the au command. For more information, visit :help filetype .

0
source share

All Articles