41

I have some log files that use color schemes. I would like to be able to view those logs in Sublime Text and still see the colors. Currently when I view the log file I see things like this:

2013-11-20T15:53:02.711Z - [34minfo[39m: Default profiles created
2013-11-20T15:53:02.712Z - [34minfo[39m: Finished server initialization
2013-11-20T15:53:02.712Z - [34minfo[39m: Start collecting garbage

The text between [34m and [39m is colored. Is anyone aware of a plugin that does this?

Thijs Koerselman
  • 21,680
  • 22
  • 74
  • 108

3 Answers3

45

Install the SublimeANSI plugin and change the syntax of your log file to ANSI (using the View -> Syntax menu.)

Also available as ANSIescape on Package Control.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
keheliya
  • 2,393
  • 2
  • 24
  • 35
30

@keheliya's answer is correct...but how to install said package?

How to install Package Control, and other packages and plugins, into Sublime Text 3 and 4+

1. Install the Package Control package

To install any other package, you need the Package Control package. Follow the installation instructions here: https://packagecontrol.io/installation

In short:

  1. Open the command palette

    Win/Linux: ctrl+shift+p, Mac: cmd+shift+p

  2. Type Install Package Control, press enter

Or, using the Menu:

  1. Open the Tools menu
  2. Select Install Package Control…

2. Use Package Control to install the ANSIescape package:

  1. Ctrl + Shift + P --> type "install", select "Package Control: Install Package" --> wait a few seconds, and a new search bar will pop up.
  2. In the new search bar --> search for "ANSIescape" and select it.
  3. Watch the status bar in the very bottom to see when it's done installing. It should just take a few seconds. Done!

Now, to use it, click the current syntax highlighting text in the very bottom right of your Sublime Text 3 window. Choose "ANSI", as shown below.

You may also choose this from the menus: View --> Syntax --> ANSI.

enter image description here

Alternative, command-line option

Instead of Sublime Text 3, you can also try less -r filename and less -R filename, but if your file has mixed color codes it may be easier to look at in Sublime Text 3 anyway. My favorite less command options are -RFX, which produce the effect seen in git diff (and also seen in my thin wrapper program I wrote to show line numbers in git diff: git diffn). For example:

less -RFX filename

Description:

  • -R to interpret ANSI color codes
  • -F to quit immediately if the output takes up less than one-screen, and:
  • -X to not clear the screen when less exits!

See:

  1. How do I prevent 'git diff' from using a pager?
  2. https://unix.stackexchange.com/questions/38634/is-there-any-way-to-exit-less-without-clearing-the-screen/38638#38638

Other, useful Sublime Text plugins I like:

  1. "Emmet" plugin to find matching HTML tags: Find closing HTML tag in Sublime Text
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • `ANSIescape` doesn't seem to exist any more when you try and use Package Manager to install? – Andrew Newby May 21 '21 at 14:39
  • I get this error when I try to set the syntax to "ANSI". `Error loading colour scheme Packages/User/ANSIescape/ansi.sublime-color-scheme: Unable to find Packages/User/ANSIescape/ansi.sublime-color-scheme` https://github.com/aziz/SublimeANSI/issues/67 Restarting sublime might fix it: https://github.com/aziz/SublimeANSI/issues/66#issuecomment-859166222 – Andrew Jun 13 '21 at 05:40
  • @AndrewNewby, I still see it. I'm using Sublime Text 4 now. – Gabriel Staples Feb 11 '22 at 16:17
  • @GabrielStaples still see the plugin as installable? If I search for it, I don't see it (using SubLime 4, build 4126) – Andrew Newby Feb 12 '22 at 07:36
  • @AndrewNewby, yes, it's still there. Are you missing the "Package Control" package? You need that first. I just updated my instructions to indicate that. – Gabriel Staples Mar 31 '23 at 18:59
  • @GabrielStaples thanks but I gave up and now use Visual Code Studio :) – Andrew Newby Apr 01 '23 at 05:34
  • @GabrielStaples nope, not there. the project description on github does say "ANSI escape codes color highlighting for ST3", so maybe that's why we can't find it on ST4. – zamarov May 20 '23 at 09:20
  • @zamarov, did you install the Package Control package first? What OS are you on? Regarding the version, a lot of packages came out when only up to Sublime Text 3 existed, but they were forwards compatible with Sublime Text 4 when it came out. Just their documentation is old. – Gabriel Staples May 20 '23 at 14:59
  • @GabrielStaples you're gonna laugh but apparently I had installed it some years ago, it just wasn't working :)) now I tried some more, edited my build system and stuff but still no colors in the terminal. I ended up switching from color-it to colorama and, while I still get no colors, at least I don't see the ascii codes in Sublime, while getting to see the correct colors in IDEs that have better terminals or even the windows cmd (I don't even need the newer and more modern Windows Terminal). – zamarov May 23 '23 at 18:46
1

PersistentRegexHighlight should be able to do what you're looking for. I'm no regex guru, so I'm not sure exactly how you'd set them up, but you could make regexes that match [34m at the beginning and [39m at the end, and highlight the middle with a blue scope.

Unfortunately, you won't be able to hide the color codes themselves, but this should at least help.

MattDMo
  • 100,794
  • 21
  • 241
  • 231