I want to make (have, actually) a custom syntax highlighting for gedit.

There would be just a few different items:

A line that begins with 2 tabs. ----- One color. A line that begins with 3 tabs. ----- Another color. A line that begins with 4 tabs. ----- Another color. and a line that begins with INT. or EXT. ----- Another Color.

This is for writing a screenplay.

I looked at the language definition wiki page for gedit but it is way over my head.

Is there a simple way I could do this?


The following was derived from the GtkSourceView reference manual, Language Definition v2.0 Tutorial and Reference.

Create the file /usr/share/gtksourceview-2.0/language-specs/screenplay.lang (as root), with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<language id="screenplay" _name="Screenplay" version="2.0" _section="Markup">
    <property name="mimetypes">text/plain</property>
    <property name="globs">*.script</property>
    <style id="indent2" _name="2x indented"  map-to="def:comment" />
    <style id="indent3" _name="3x indented"  map-to="def:constant" />
    <style id="indent4" _name="4x indented"  map-to="def:identifier" />
    <style id="intext"  _name="INT. or EXT." map-to="def:statement" />
    <context id="screenplay">
        <context id="indent4" style-ref="indent4">
        <context id="indent3" style-ref="indent3">
        <context id="indent2" style-ref="indent2">
        <context id="intext" style-ref="intext">

Make sure the file is world-readable by running

sudo chmod 0644 /usr/share/gtksourceview-2.0/language-specs/screenplay.lang

Restart gedit. Now any file named *.script that you open in gedit should show the type "Screenplay" in the status bar at bottom. Lines indented with 2, 3, or 4 tabs should be colored blue, magenta, and cyan, respectively, and lines beginning with INT. or EXT. will be red and bold-face.

You can adjust this in a few ways:

  • The .script file name suffix is set on line 5, in the "globs" property. You can change this in the obvious way, and include multiple values if you want, separated by semicolons (;).

  • The colors that you get for the indented and INT. and EXT. lines depend on the particular color scheme that you've selected for gedit. They're the colors of the comment, constant, identifier, and statement styles (that's what map-to="def:comment" etc. do). To get different results, you can change the gedit color scheme (Edit > Preferences > Fonts and Colors), or rearrange the map-to values in the <style> tags. If you want more control, you can create your own color scheme as follows:

    cd /usr/share/gtksourceview-2.0/styles
    sudo cp classic.xml screenplay.xml

    Edit screenplay.xml (as root). Replace the first three non-comment lines by:

    <style-scheme id="screenplay" _name="Screenplay" version="1.0">
      <_description>Screenplay color scheme</_description>

    Then adjust the colors (and maybe other styles too, e.g. bold="true") of the def:comment, def:constant, def:identifier, and def:statement styles. Restart gedit again, and change gedit's color scheme to Screenplay to see your new colors. Note that that's a global change though; you may want to change back to your regular color scheme to edit other files.

Note that any time you change the screenplay.lang or screenplay.xml files, you'll have to restart gedit to see the results.

  • That did it! AWESOME! – YAS Nov 7 '11 at 15:18
  • Thanks, I had a similar use case, highlighting related but physically disparate lines with the same color and this helped a lot. – Eloff Dec 6 '12 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.