Sublime Text 3 - custom syntax highlighting
- From the sandbox
- Tutorial
Extensibility Sublime Text knows no bounds. For those who have few standard functions, on Habré they already talked about how to create a snippet , how to write a simple plug-in , how to write a complex plug-in, and much more. I couldn’t really find anything about manually adjusting syntax highlighting: for someone it’s too obvious, someone just doesn’t need it, but someone will surely come in handy.
So, the task: having any abstract access logs in wonder-net:
Highlight:
Date-time - in purple italics;
Card number - in yellow italics, if not in the black list or in red, if not;
ip-address - dark green, if not in the black list or red, if otherwise;
scod = xx - dark green if 00 or red if otherwise.
In our color scheme, we need to add styles with which we are going to highlight the text. As I understand it, all color schemes are neatly collected in the file c: \ Program Files \ Sublime Text 3 \ Packages \ Color Scheme - Default.sublime-package.
The file is a zip archive without compression. Unpack, select your favorite scheme (by default Monokai.tmTheme) and copy it to ... (run Sublime Text: Preferences menu - Browse Packages) User folder.
Open the freshly copied Monokai.tmTheme for editing. At the bottom, before the closing, add style blocks. A style can contain three parameters: background, foreground and fontStyle. fontStyle in turn is bold , italic and underline .
purple italics will look like this:
bold yellow (LJ):
dark green:
red:
2. In the same User folder, create the syntax file: wonderLog.tmLanguage with the contents:
The example shows that if several regular expressions pretend to paint the color differently, then the priority will be the one described above (first).
We save everything.
Select a custom color scheme: Preferences - Color Scheme - User - Monokai.
We open our miracle log (if the file extension does not match the template, do View - Syntax - wonderLog) and enjoy the result:

So, the task: having any abstract access logs in wonder-net:
!->14/0216:44:22 [134.249.51.251:39951>80] (t1 19) >HTTP in:504out:34Time:156GET /516874233**21893/ HTTP/1.1 SCOD=00
!->14/0216:44:24 [134.249.51.251:49507>80] (t1 20)
GET /44058858**409377/ HTTP/1.1 SCOD=00
!->14/0216:54:11 [195.18.13.107:1721>80] (t2 22)
GET /41494377**562173/ HTTP/1.1 SCOD=00
!->14/0216:54:11 [195.18.13.107:1721>80] (t2 23) >HTTP in:385out:10138Time:156GET /5211537**1172048/ HTTP/1.1 SCOD=00
!->14/0216:54:24 [195.18.13.107:1727>80] (t1 30) >HTTP in:423out:1220Time:187GET /5211537**6447554/ HTTP/1.1 SCOD=23
!->14/0218:07:24 [82.145.208.159:43634>80] (t2 52)
GET /4149437**8265377/ HTTP/1.1 SCOD=00
!->14/0218:09:41 [82.145.208.174:41708>80] (t2 61)
GET /4149497**5750155 / HTTP/1.1 SCOD=00
!->14/0218:51:39 [82.145.210.33:55210>80] (t1 66) >HTTP in:543out:34Time:0GET /5168757**9478487/ HTTP/1.1 SCOD=00
!->14/0218:51:40 [82.145.210.33:55332>80] (t1 68) >HTTP in:544out:1243Time:141GET /4149497**0456701 / HTTP/1.1 SCOD=00
!->14/0218:51:46 [82.145.210.33:57345>80] (t1 73) >HTTP in:544out:1243Time:125GET /5168742**0521893/ HTTP/1.1 SCOD=00
Highlight:
Date-time - in purple italics;
Card number - in yellow italics, if not in the black list or in red, if not;
ip-address - dark green, if not in the black list or red, if otherwise;
scod = xx - dark green if 00 or red if otherwise.
In our color scheme, we need to add styles with which we are going to highlight the text. As I understand it, all color schemes are neatly collected in the file c: \ Program Files \ Sublime Text 3 \ Packages \ Color Scheme - Default.sublime-package.
The file is a zip archive without compression. Unpack, select your favorite scheme (by default Monokai.tmTheme) and copy it to ... (run Sublime Text: Preferences menu - Browse Packages) User folder.
Open the freshly copied Monokai.tmTheme for editing. At the bottom, before the closing, add style blocks. A style can contain three parameters: background, foreground and fontStyle. fontStyle in turn is bold , italic and underline .
purple italics will look like this:
<dict><key>scope</key><string>violet</string><!-- По этому имени будем обращаться к стилю --><key>settings</key><dict><key>foreground</key><string>#EE82EE</string><key>fontStyle</key><string>italic</string></dict></dict>
bold yellow (LJ):
<dict><key>scope</key><string>yellow</string><key>settings</key><dict><key>foreground</key><string>#FFD700</string><key>fontStyle</key><string>bold</string></dict></dict>
dark green:
<dict><key>scope</key><string>green</string><key>settings</key><dict><key>foreground</key><string>#006400</string></dict></dict>
red:
<dict><key>scope</key><string>red</string><key>settings</key><dict><key>foreground</key><string>#FF0000</string></dict></dict>
2. In the same User folder, create the syntax file: wonderLog.tmLanguage with the contents:
<?xml version="1.0" encoding="UTF-8" ?><plistversion="1.0"><dict><key>patterns</key><array><dict><key>name</key><string>red</string><!-- Имя стиля --><key>match</key><string>\b(?i)(?:5168742**0521893|414943**01562173|4149497**5750155)\b</string><!-- Регулярное выражение к-рое надо выделить (номера из черного списка) --></dict><dict><key>name</key><string>red</string><key>match</key><string>\b(?i)(?:134\.249\.51\.251|82\.145\.208\.174)\b</string><!-- ip из черного списка (точки экранируем) --></dict><dict><key>name</key><string>green</string><key>match</key><string>\b(?i)scod=00\b</string><!-- scod=00 --></dict><dict><key>name</key><string>red</string><key>match</key><string>\b(?i)scod=\d+\b</string><!-- scod!=00 --></dict><dict><key>name</key><string>green</string><key>match</key><string>\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b</string><!-- остальные ip (конечно, надо не так, но...) --></dict><dict><key>name</key><string>yellow</string><key>match</key><string>\b\d{16}\b</string><!-- остальные номера --></dict><dict><key>name</key><string>violet</string><key>match</key><string>\d{1,2}/\d{1,2}\s\d{2}:\d{2}:\d{2}</string><!-- дата, время --></dict></array><key>name</key><string>wonderLog</string><key>scopeName</key><string>wonderLog</string><key>fileTypes</key><array><string>log</string><!-- К этому расширению файла будет автоматически применяться данный синтаксис --></array></dict></plist>
The example shows that if several regular expressions pretend to paint the color differently, then the priority will be the one described above (first).
We save everything.
Select a custom color scheme: Preferences - Color Scheme - User - Monokai.
We open our miracle log (if the file extension does not match the template, do View - Syntax - wonderLog) and enjoy the result:
