Vim text coloring
Asked Answered
H

3

9

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

But when I redirect the output to a file, vim doesn't show the colors, instead of that it shows signs like this ^[[

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

NB: cat shows the colors too!!

Honeysuckle answered 8/9, 2013 at 13:48 Comment(2)
Note that cat doesn't show the colors, it simply outputs (without the interpretation that Vim does) the contents to the terminal. The terminal translates the escape sequences into colors!Counterclockwise
either use AnsiEsc if your text is usually going to be outputted by the terminal directly (usually via a shell), or, if you intending it to be viewed in vim only, colorized, (AnsiEsc messes up on size calculations and it's annoying), you may want to check out the :syntax and :hi commands, which will enable you to highlight/colorize text with regular expressions, much easier to manage it that way (unless of course, you are dealing with ANSI art or something like that).Stela
D
7

Use either the AnsiEsc Plugin by Dr. Chip or my Colorizer plugin. Both should be able to color those Terminal Escape sequences.

Danais answered 8/9, 2013 at 14:43 Comment(0)
C
2

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

Because it is a text editor, it is reasonable to expect Vim to display the content of the file as it is, with the escape characters, rather than as you want it to look.

So no, there's nothing to fix, here. On Vim's side, anyway.

Conatus answered 8/9, 2013 at 16:23 Comment(0)
U
0

Redirecting to a file creates that file with all the characters in the input -- including the color escapes. This is actually correct behaviour, and vim is showing you the right thing when it shows these special characters to you.

It seems that perhaps what you're looking for is syntax highlighting. Vim comes with the ability to understand and colorize many different types of text.

Try making sure you have the filetype option correctly set for whatever type of script you're using. Once you get it, you can get vim to set it automatically with the au command. Check out :help filetype for more info.

Unifoliolate answered 9/9, 2013 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.