Create a custom syntax highlight in gedit

6,459

Create and open your fortune language file:

sudo touch /usr/share/gtksourceview-3.0/language-specs/fortune.lang
sudo -i gedit /usr/share/gtksourceview-3.0/language-specs/fortune.lang

Paste the following:

<?xml version="1.0" encoding="UTF-8"?>
<language id="fortune" _name="Fortune" version="2.0" _section="Markup">
  <metadata>
    <property name="mimetypes">text/plain</property>
    <property name="globs">*.</property>
  </metadata>

  <styles>
    <style id="at"      _name="@ sign"  map-to="def:constant" />
    <style id="percent" _name="percent sign"  map-to="def:comment" />
  </styles>
  <definitions>
    <context id="fortune">
      <include>
        <context id="at" style-ref="at">
          <start>@</start>
          <end>$</end>
        </context>
        <context id="percent" style-ref="percent">
          <start>%</start>
          <end>$</end>
        </context>
      </include>
    </context>
  </definitions>
</language>

Make sure it is accessible:

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

Restart gedit.

Reference - my source answer

My pronouns are He / Him

Share:
6,459

Related videos on Youtube

Parto
Author by

Parto

Far better it is to dare mighty things, to win glorious triumphs, even though checkered by failure, than to take rank with those poor spirits who neither enjoy much nor suffer much, because they live in the gray twilight that knows neither victory nor defeat. ― Theodore Roosevelt

Updated on September 18, 2022

Comments

  • Parto
    Parto over 1 year

    Following my question from long ago, I am still trying to tweak my fortune handling capabilities. The display in terminal is awesome, just one thing remains - syntax highlighting in Gedit as I add, edit or delete individual fortunes.

    My custom fortune files are in this format:

    Friend: "Whats a good movie?"
    Me: "Snakes on a plane"
    Friend: "Whats it about?"
    Me: "Horses... horses on a boat..."
    @AYYSIAN
    %
    Me on my wedding: you still like me, right?
    @ComedyPosts
    %
    Mum: Son I'd love to meet your girlfriend...
    Me: Me too...
    @ComedyTruth
    %
    Doctor: "Ok, so what's wrong, how are you feeling?"
    Me: *Looks at mum waiting for her to explain*
    @ChildhoodFact
    %
    Friend: 75% of people are good at maths...
    Me: Mmmmh, I guess then am in the remaining 18%...
    @TheFunnyTeens
    %
    I loved the Titanic. My favorite character was the iceberg
    @__GrumpyCat
    

    It's basically a tweet, @name and the % character. Then repeat.

    When I open it in Gedit, everything is black.
    I'm looking for a way to create a syntax highlighting file that will convert the % to something like blue and the @name to something like magenta. The tweet can remain black.

    NOTE:

    1. The fortune files are mime-type text/plain and only the given format really distinguishes them from another plain text file.
    2. Fortune files have no extension.
  • Parto
    Parto almost 10 years
    Thanks alot, will try this out. One thing though, fortune files have no extension, the reason for the text/plain mime type. Does that mean <property name="globs">*.fort</property> becomes <property name="globs">*</property>?
  • Tim
    Tim almost 10 years
    @Parto That would apply it to all files with no extension, but as most are .txt that would be fine. Change it to <property name="globs">*.</property>, and I've updated the answer with this!
  • TRiG
    TRiG over 7 years
    Massive thanks. I just used this to add Twig syntax highlighting to gedit.
  • Tim
    Tim over 7 years
    @TRiG Glad to have helped!