A better Python
.tmLanguage syntax highlighting definition for Sublime Text and TextMate. It includes support for both Python 2 and Python 3, and unlike any other Python syntax definition now fully supports Unicode identifiers anywhere in your code! It also provides its own improved regex syntax definition for inline highlighting of raw string literals.
as well as a number of my own changes to make things more consistent and understandable. For customized syntax highlighting taking advantage of all the new scopes, use PythonImproved with the Neon Color Scheme, or modify your own favorite color scheme with the scopes below.
If you haven't already, install Package Control, then select
Python Improved from the
Package Control: Install Package option in the Command Palette. To use PythonImproved as your default Python syntax, open a
.py file, then select
View -> Syntax -> Open all with current extension as... -> PythonImproved.
While I haven't yet tried to install PythonImproved with TextMate, I can't think of a good reason as to why it wouldn't work. You could try putting it in the same directory as the standard
Python.tmbundle package, in the
Syntaxes subdirectory. Then, just pick
PythonImproved from the syntax menu.
If you prefer to modify your own color scheme, here is a list of new/modified scopes, along with some examples. It's not perfectly complete, but it's a start.
Out :fields — designed for use with SublimeREPL. The cell number can be themed with a different color using
constant.numeric.integer.(long).binary.python: binary literals
support.type.exception.pythonnow matches any identifier that ends with
Error, not just the built-in ones like
RuntimeException, allowing for the highlighting of custom exceptions such as those included in third-party modules.
def myfunc(self, # gotta have self
param1="value", # values are cool
param2=True, # or False, whatever
*args, # I'm here for an argument
**kwargs): # you never know
self|clsin parameter strings:
comment.line.note.pythonis a comment line that contains
(BUG|FIXME|TODO|XXX)at the beginning.
comment.line.note.notation.pythonmatches the actual word itself, so you can differentially highlight the word and the whole line:
constant.other.allcaps.pythoncaptures variable names that are in all caps (
OPENING_PORT, for example), assuming the convention that these are generally treated as constants in the code. Matches
CLASS.CONSTANT, but not
0o123is matched as well as the old-style
raw_input(), etc. now have their arguments highlighted just like any other function. Many thanks to @facelessuser for the regex, and @FichteFoll for valuable discussion. For those working with Python 2,
support.function.builtinhave now been split in two —
call, so that
support.function.magic.name.python), for example, can be themed differently than
support.typenow contains only what's defined in https://docs.python.org/X/library/functions.html and stdtypes.html (where
3) where the item is a class. They are highlighted as such only if not followed by an opening parenthesis — if it is, it's highlighted as
support.function.builtin.call. This addresses #16.
\\, etc.) are now individually named as
.YAML-tmLanguagefile in the repo, which I use for my day-to-day work (I really hate debugging regexes embedded in XML). Install
PackageDevfor syntax highlighting, and tools for converting between YAML, JSON, and XML/Plist formats. Neon of course has great coloring for the
.YAML-tmLanguageformat, and especially the regexes :)
Regular Expressions (Python Improved).tmLanguagefile, instead of the builtin Python regular expressions definition. If you're using Neon for syntax highlighting (or any color scheme that highlights regexes), use a lowercase
rto denote your string as containing a regex (i.e.,
r"\b(?i:(0[o]?[0-7]+))"). However, if you're just using a raw string literal to, for example, define a Windows path and you don't want regex highlighing for all the back slashes and whatnot, use an uppercase
R"C:\Users\MattDMo"). Python can't tell the difference, but it will look nicer in your editor.
.sublime-syntaxfiles, I'm going to begin transitioning PI over to that format. If you'd like to contribute, chime in on this issue. One major advantage will be fixing this bug with raw string literals.
If you have questions, concerns, or suggested improvements, I'd love to hear from you! Feel free to open an issue or send a pull request and I'll get back to you as soon as I can. You can also email me at email@example.com or find me on Twitter @MattDMo.
© 2013-2022 Matt Morrison firstname.lastname@example.org.
This is free software. It is licensed under the MIT License. Feel free to use this in your own work. However, if you modify and/or redistribute it, please attribute me in some way, and it would be great if you distribute your work under this or a similar license, but it's not required.. A shout-out or a beer would be appreciated.
PythonImproved 3.0 work has officially started... with this tracking issue. Gotta start somewhere.
.sublime-syntax(unofficial docs). The
.YAML-tmLanguagefiles can be renamed
.sublime-syntaxwithout issues, or translated to
.tmLanguagePLIST format with
.sublime-syntaxenhancements for readability/maintainability
=specifier from 3.8
awaitin generators and comprehensions, are reserved keywords as of 3.7
:=walrus operator for assignment expressions
I'm trying to make a parser that will create a html div of some code to put into blogs and for it to look like sublime's formatting. I manually made mine on my own website with some basic regex searches, I'm wondering if I could tap into the code from this to get a parser? If theres something out there already thats easy to modify then please tell me I'm wasting my time! Heres and example on my recent blog
It's just created with lots of span's and some css
Sometimes I get for first letters of words another scopes.
For example, I edit file
color_theme.py, lines 13—26.
The same scopes for
parsers word and so on.
etree word I have a scope
e letter and
source.python meta.identifier.python for
tree letters and so on.
Operating system and version: Windows 10 Enterprise LTSB 64-bit EN Sublime Text: Build 3126
Hi! I have created a custom theme and syntax highlighting for linguistic purposes that work very well — they change text color according to rather complex regexes.
However, I want to change the background color for given strings — any two or more spaces, to be exact. How to do it?
I am talking about such effect: http://imgur.com/a/gwftx
I have the following (ugly) statement
arrays = sorted(filter(
lambda x: not any(y for y in ['NOMON', 'NEW'] if y in x) and
any(y for y in ['-AN', '-P2P'] if y in x),
[x['hostname'].encode().upper() for x in r.json()]))
Matching parenthesis do not highlight properly in PythonImproved, but work just fine with Python.
With the cursor at the end:
Cursor in the middle:
Cursor at the lambda:
PythonImproved does not treat any lines after this statement as though they were parameters to the
sorted methods, so it appears to be an aesthetic issue. (Code works fine.)
got rid of
Regular Expressions (Python Improved).tmLanguage's suffix to
.hidden-tmLanguage so it doesn't show up in the Syntax menus, allowing for more convenient selection of the syntax.
messages/2.2.0.txt for changes.
Hopefully this is the last bugfix for
This is a quick bugfix release for the 2.1 series.
cls(arg=val, ...) now has proper highlighting for the function arguments. Some highlighting regressions which resulted from trying to address the above in 2.1.0 were fixed as well.
Lots of bug fixes in this version, as expected after a new major release. I suppose one of the most user-facing changes is the reversion back to the old style of highlighting raw/regex string literals.
r"blah" will still be scoped as before, with regex highlighting (if any is present). However, you now have the option of declining regex highlighting by using a capital R (
R"blah") instead. Python doesn't care if the R is in caps or not, it simply treats the string as a raw string literal.
Other changes include fixing magic functions (again) so that function calls are properly scoped as
support.function.magic.call while magic function definitions are
support.function.magic.name. If you use Neon the highlighting is still identical, for now. I also fixed an edge case where using
cls as a function call broke highlighting of the arguments (
self can now be used as well, obviously assuming
__call__ has been implemented in your class). I also got rid of some unnecessary(?) code that wasn't, as far as I could tell, actually doing anything. If something breaks, please tell me. I've also added
tests/test_code.py which is just a bunch of random snippets for testing. Finally, I've made some changes to the README, mostly updating (or adding) URLS, as well as detailing all of the above.
I invented the question mark.GitHub Repository Homepage
sublime-text python syntax-highlighting sublime-syntax tmlanguage sublime-text-3 python3 python2 unicode