Syntax highlighting for Python in gedit

Solution 1:

I realize I'm quite late to the party, but I had a similar issue and this thread was the first that popped up. So I thought I would add a thorough answer explaining what is going on.

The best documentation I found for how the highlighting works is the documentation for GTKSourceView (, which is what Gedit uses for highlighting): https://www.manpagez.com/html/gtksourceview/gtksourceview-3.12.2/ch02.php But it doesn't answer OP's question directly.

So to answer OP's question more directly. I don't know of a simple list, but you can find the information you are looking for if you know a bit about how the highlighting works.

There are two important parts to how the highlighting works:

  1. The .lang file (there is one for each supported programming language), which defines how Gedit (or rather GTKSourceView) should understand the syntax of the programming language. i.e. in python.lang it is defined, that # marks a comment, "for", "if" and "else" are keywords, '...' is a string and so on.

  2. The color scheme then tells Gedit how to color a generic comment, a generic keyword and so on, based on how comments, keywords, strings and so on are defined in the .lang file of the language you are currently using.

What language you are using is specified in the bottom of Gedit: Language selection dialog currently python (2.x)

What color scheme you are using is set under: menu -> preferences -> Font and Colors -> Color Scheme Color Scheme dialog

The .lang files are (probably) found in:

/usr/share/gtksourceview-3.0/styles/language-specs

or

/usr/share/gtksourceview-4/styles/language-specs

depending on your version of GTK.

and the The color scheme/styles are in:

/usr/share/gtksourceview-3.0/styles/

or

/usr/share/gtksourceview-4/styles/

again depending on your version of GTK.

So to answer OP's question. You would go to the .lang file for the language you are interested in e.g. python.lang or python3.lang. There at the top you will find a list of styles looking like:

    <style id="keyword"           name="Keyword"               map-to="def:keyword"/>

This says, that the coloring style for python keywords (which are defined later in the python.lang file) should be colored in the same way as "def:keyword", which is the is the default for keywords in any supported language ("def" for default, def: refers to the default styles which are found in def.lang).

The definition of what a keyword in python is can then be found later if you search for 'style-ref="keyword"' in a block like:

<context id="keywords" style-ref="keyword">
  <keyword>and</keyword>
  <keyword>assert</keyword>
  ...

Here 'id="keywords", means that the following entries are what we define as python keywords, 'style-ref="keyword" means that the things listed bellow should be colored with the python keyword coloring style, and then it lists the keywords: "and", "assert", "if", "for" and so on.

Now we know from the .lang files what the different styles are called and what is colored with which styles. Now we can specify in the color scheme which actual colors to use for all the abstract styles, that has been defined in the .lang files.

In the color theme files e.g. tango.xml you will find lines like:

  <style name="def:keyword"                 foreground="#0000ff"/>

"def:keyword" is the default keyword style which is used to color things according to the .lang files. and 'foreground="#0000ff" means, that the text will be colored with the RGB hex value 00-00-ff - i.e. pure blue.

This will color the python keywords blue, since the keyword style defined in python.lang was mapped to def:keyword which we now declared should be blue.

It should also color keywords from all other languages blue (so long as they are also mapped to def:keyword in their .lang file).

You can also specify a color for the python keywords specifically, by writing:

  <style name="python:keyword"                 foreground="#00ff00"/>

where the part part before the colon is the id of the language (written at the top of the .lang file) and the part after the colon is the name of the style in that .lang file.

The styles which are not given a color, will be decided by your default color theme for Gnome/Ubuntu in a way, that I sadly do not understand ¯_(ツ)_/¯

Solution 2:

This is related to which color scheme you choose in Font & color prefrences of your gedit. For me I choose Classic

enter image description here

Now to see how it highlights open the xml of the color scheme

gedit /usr/share/gtksourceview-3.0/styles/classic.xml 

Here is a sample of output:

<!-- Bracket Matching -->
  <style name="bracket-match"               foreground="#white" background="#grey" bold="true"/>
  <style name="bracket-mismatch"            foreground="#white" background="#red" bold="true"/>

  <!-- Search Matching -->
  <style name="search-match"                background="yellow"/>  

  <!-- Comments -->
  <style name="def:comment"                 foreground="blue"/>
  <style name="def:shebang"                 foreground="blue" bold="true"/>
  <style name="def:doc-comment-element"     italic="true"/>

Then you can know that grey color is used for bracket matching and blue color for comments and yellow for search, etc...

You can check the other schemes from their xml files founded in the path:

/usr/share/gtksourceview-3.0/styles/

Also, as a hint you can use this xml to change the colors as you like so you can customize each scheme to your needs or likes