27

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?

Michael Martin-Smucker
  • 6,572
  • 4
  • 29
  • 57
  • 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. – nickguletskii May 23 '11 at 15:32

4 Answers4

21

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>

Kris Harper
  • 13,397
  • 12
  • 56
  • 73
jdup
  • 226
  • 2
  • 2
  • Thanks! This worked for making gedit add syntax coloring to my ColdFusion files. – Michael Martin-Smucker Oct 11 '11 at 16:31
  • 2
    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. – Michael Martin-Smucker May 29 '12 at 12:53
  • 1
    I like the idea of duplicating and editing the `.lang` in the [user directory](http://askubuntu.com/questions/66601/why-did-all-my-custom-gedit-themes-and-language-specs-disappear-after-the-11-10) myself – jozxyqk Aug 28 '14 at 12:05
  • 1
    @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: ```*.css;*.CSSL;*.less;*.sass``` – LunkRat Jan 06 '15 at 20:08
  • You may have to modify the path for different versions of gtksourceview. You may also want to copy the file to your profile folder to prevent it being overwritten. See 5chdn's answer below (http://askubuntu.com/a/310553/37574) – mwfearnley Aug 10 '16 at 13:48
  • I'm on Ubuntu 22.10 and there is no `html.lang` under `/usr/share/gtksourceview-2.0/language-specs/` -- only `nemo_action.lang` and `nemo_search_helper.lang`. UPDATE: Nevermind, apparently it's now in gtksourceview-5.0. :) – odigity Apr 05 '23 at 17:39
16

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.

q9f
  • 1,221
  • 3
  • 18
  • 29
9

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.

Lucio
  • 18,648
  • 31
  • 107
  • 190
ændrük
  • 75,636
  • 74
  • 233
  • 365
  • 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 May 23 '11 at 20:15
  • 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.) – Michael Martin-Smucker May 23 '11 at 20:25
  • 1
    @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`](http://live.gnome.org/Gedit/PythonPluginHowTo#Gedit.Document) call into the [sample Python plugin](http://live.gnome.org/Gedit/PythonPluginHowTo#Minimal_plugin). You can probably use `get_language` to figure out the right value for HTML source highlighting. – ændrük May 23 '11 at 20:35
  • I created a feature request for making this default configurable, you can follow it here: https://gitlab.gnome.org/GNOME/gedit/-/issues/492 – Erik Brendel Feb 21 '22 at 14:46
0

There is a conflict in the files /usr/share/gtksourceview-3.0/language-specs/sml.lang and /usr/share/gtksourceview-3.0/language-specs/xml.lang

Both have the .sml extension associated with it. To get Standard ML syntax highlighting automatically you need to erase .sml from /usr/share/gtksourceview-3.0/language-specs/xml.lang.