Can I set a default syntax highlighting in Gedit?

14,559

Solution 1

You can at least add file extensions in the html syntax coloring scheme by editing html.lang in /usr/share/gtksourceview-2.0/language-specs/ as a super user. So say you want to add HTML syntax highlighting to cfm files, you'd change this

<property name="globs">*.html;*.htm</property>

(default html.lang) into this

<property name="globs">*.html;*.htm;*.cfm</property>

Solution 2

You can create your own language definition files in your $HOME directory. Editing the gtk language specs is not recommended as it will be overriden by every gtksourceview update.

Copy your language specs file to your home directory:

$ cp /usr/share/gtksourceview-3.0/language-specs/html.lang ~/.local/share/gtksourceview-3.0/language-specs/

Then, add your custom file formats like .cfm to the list of extensions:

<property name="globs">*.html;*.htm;*.cfm</property>

That way gedit will use auto syntax highlighting on your custom file formats and gtk updates wont override your custom changes.

Solution 3

This is not possible without modifying the source code of Gedit or writing a plugin to override the default. The default syntax highlighting scheme is hard-coded.

Share:
14,559

Related videos on Youtube

Michael Martin-Smucker
Author by

Michael Martin-Smucker

Updated on September 18, 2022

Comments

  • Michael Martin-Smucker
    Michael Martin-Smucker over 1 year

    Until you save a document in gedit, syntax highlighting is turned off. There are obviously good reasons for this -- people might get confused if certain words were randomly showing up in different colors. But for my purposes, I use gedit almost exclusively for HTML editing.

    A lot of times I paste snippets of code into a new gedit document for quick editing, and I have to manually set the syntax coloring to HTML. Other times, I open ColdFusion (.cfm) documents, which gedit apparently doesn't recognize, and again I have to manually set the color to HTML. Both of these inconveniences would be fixed if I could find a way to tell gedit to automatically use HTML syntax highlighting for new documents and documents without a recognized file extension. Is this possible?

    • Jose Silva
      Jose Silva almost 13 years
      I was also wondering if I could switch between different languages without having to save the file, just like in SciTE. Would save me time.
  • Michael Martin-Smucker
    Michael Martin-Smucker almost 13 years
    Thanks for pointing me in the right direction. I cloned the latest gedit and started poking around before I realized that gedit now depends on gtk+ 3. This has the potential to turn into a much bigger mess than I was bargaining for. ;-)
  • Michael Martin-Smucker
    Michael Martin-Smucker almost 13 years
    One more thing: any idea what values for language are valid? I looked through the source for gedit and gtksourceview and I couldn't find anything that looked like language names (but my C skills are pretty non-existent.)
  • RusGraf
    RusGraf almost 13 years
    @MichaelMS I'm not sure, but if you plan on investing any effort into a solution I honestly think it may be easiest to just implant a set_language call into the sample Python plugin. You can probably use get_language to figure out the right value for HTML source highlighting.
  • Michael Martin-Smucker
    Michael Martin-Smucker over 12 years
    Thanks! This worked for making gedit add syntax coloring to my ColdFusion files.
  • Michael Martin-Smucker
    Michael Martin-Smucker almost 12 years
    I wish I could add a few extra up-votes to this answer. I just used this answer again to give CSS highlighting to LESS and SASS files.
  • jozxyqk
    jozxyqk over 9 years
    I like the idea of duplicating and editing the .lang in the user directory myself
  • Link Swanson
    Link Swanson over 9 years
    @michaelms I added an upvote to the post and to your comment because I also added CSS syntax highlighting to my LESS files in gedit by editing sudo nano /usr/share/gtksourceview-3.0/language-specs/css.lang and adding *.less and *.sass to the globs property: <property name="globs">*.css;*.CSSL;*.less;*.sass</property>
  • nightcod3r
    nightcod3r over 7 years
    This is a better option than the selected answer. When possible, apply changes locally.
  • Srikrishnan Suresh
    Srikrishnan Suresh about 6 years
    But how will gedut recognize the copied file in $HOME? How do I tell gedit that the .lang file in $HOME needs to be looked into??? Thanks in advance.
  • Erik Brendel
    Erik Brendel about 2 years
    I created a feature request for making this default configurable, you can follow it here: gitlab.gnome.org/GNOME/gedit/-/issues/492