How can I make gedit recognize a document type permanently?

Hi.

Ive downloaded a set of wiki source files, which end in .mediawiki. The editor that gives me the interface I like best is gedit with document type “MediaWiki”.

However I have to change the document type with every single new file ! After I reopen the same file, the document type is set like I selected it, fortunately. But I have several dozens of files and I’m not going to change the document type with every single one of them…

Is there a way to have gedit permanently “remember” all files ending in .mediawiki are of type “MediaWiki” ?

Thanks in advance for any hint/suggestion.

EDIT: I forgot to add (if that helps) I’m using Xfce as my desktop environment.

Using Thunar, I would expect you to Right Click the file, and select Properties - then ‘Open With…’ and set it there with Gedit as the default.

Otherwise, I’d say you have something like ~/.local/share/applications/defaults.list

Using a terminal, you can try the mimeopen application…

❯ mimeopen -d flogthatmonkey.mediawiki 
Please choose a default application for files of type text/plain

        1) Kate  (org.kde.kate)
        2) LibreOffice Writer  (libreoffice-writer)
        3) Micro  (micro)
        4) Okular  (okularApplication_txt)
        5) ghostwriter  (org.kde.ghostwriter)
        6) KWrite  (org.kde.kwrite)
        7) Reader  (org.kde.mobile.okular_txt)
        8) Vim  (vim)
        9) Audacious  (audacious)
        10) Detect It Easy  (die)
        11) Other...

use application #

I’m not actually sure if this will set for all text files, however - I’m not sure how to focus purely on the file extension.

The association is already set for gedit. It’s the colouring syntax that is set to “Plain text” by default instead of “Media Wiki”.

1 Like

Ah, I understand now - sorry. Barking up the wrong tree.

It’s okay. I’ve seen from the “Default applications” dialogue there’s a text/markdown document type but the thing is it’s also tied to gedit. If there’s a way to create a new MIME type, e.g. x-application/mediawiki, would gedit switch to the adequate syntax colouring scheme?

EDIT: Tried that. Failed :frowning:

The custom MIME type is indeed registered and shows up with xdg-mime query filetype and as expected gedit opens when summoned with xdg-open <mediawiki file>. But syntax colouring always defaults to text/plain… *sighs*

Here’s the descriptor I added to .local/share/mime/packages:

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                                    
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">                                                                                                 
  <mime-type type="text/x-mediawiki">                                                                                                                     
    <glob pattern="*.mediawiki"/>                                                                                                                                               
    <comment>MediaWiki document (markdown)</comment>                                                                                                                      
      
  </mime-type>                                                                                                                                                            
</mime-info>                                                                                                                                                              

Then I ran update-mime-database .local/share/mime.

I think it’s worth a try - I’d like to do this for a few files that open in Kate as ‘normal’ but would benefit from some formatting.

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/vnd.mediawiki">
        <comment>WikiMedia file type</comment>
        <glob pattern="*.wikimedia"/>
    </mime-type>
</mime-info>

For kde this would go /usr/share/mime/packages

sudo update-mime-database /usr/share/mime

Then associate the file and restart the session.

Doesn’t seem to work, as described in my previous post. I’d tend to be more confident with Kate however. KDE and Gtk being totally different beasts…

EDIT: I didn’t restart my session though… One moment, please…

1 Like

I just pasted this text in my dummy file… it seems Kate already recognises the extension!

However, after opening that same file with Gedit (fresh install) and setting it to MediaWiki… closing it out, reloading it also comes back:
Screenshot_20250104_210150

Well, I closed my session (after making sure I’d set up my local MIME database correctly, which I did in the end) aaaaand… no luck. Still doesn’t work and gedit, just like mousepad would default to `text/plain :frowning:

Here’s a sample contents from the database I downloaded (it’s from SDL2):

= SDL_LogMessage =

Log a message with the specified category and priority.

== Syntax ==

<syntaxhighlight lang='c'>
void SDL_LogMessage(int category,
                    SDL_LogPriority priority,
                    SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(3);
</syntaxhighlight>

== Function Parameters ==

{|
|'''category'''
|the category of the message
|-
|'''priority'''
|the priority of the message
|-
|'''fmt'''
|a printf() style message format string
|-
|'''...'''
|additional parameters matching % tokens in the '''fmt''' string, if any
|}

== Version ==

This function is available since SDL 2.0.0.

== Related Functions ==

:[[SDL_Log]]
:[[SDL_LogCritical]]
:[[SDL_LogDebug]]
:[[SDL_LogError]]
:[[SDL_LogInfo]]
:[[SDL_LogMessageV]]
:[[SDL_LogVerbose]]
:[[SDL_LogWarn]]

----
[[CategoryAPI]], [[CategoryLog]]

However, after opening that same file with Gedit (fresh install) and setting it to MediaWiki… closing it out, reloading it also comes back:

It does indeed. However it doesn’t switch to Mediawiki colour mode/syntax when opening files it hasn’t seen yet, which is the burden I’d like to avoid. I have no idea why gedit (as well as mousepad, by the looks of it) makes the association on a per-file basis instead of per-extension. That’s beyond me.

EDIT: From that comment on Ubuntu Stack Exchange, both editors are would be based on gtksourceview, so I’ll try fiddling with its language descriptor files and see what happens…

EDIT: contrary to what I (and said comment) expressed gedit is not based upon the system package gtksourceview but uses its own spin, namely libgedit-gtksourceview-300… Investigating further indeed…

I finally found the solution, hence I’m posting this.

Indeed gedit is based on libgedit-gtksourceview:

pacman -Qi gedit | grep -Eoi '\S+sourceview'
libgedit-gtksourceview

That library does support mediawiki language syntax:

pacman -Ql libgedit-gtksourceview | grep mediawiki
libgedit-gtksourceview /usr/share/libgedit-gtksourceview-300/language-specs/mediawiki.lang

So I copied that file to .local/share/libgedit-gtksourceview-300/language-specs and added the following two lines to the <metadata> tag (first tag in the file):

    <property name="mimetypes">text/x-mediawiki</property>
    <property name="globs">*.mediawiki</property>

Then I closed gedit and opened a mediawiki file that I never opened yet and *bam* syntax colouring was set to “MediaWiki”!

Problem solved :sunglasses:

BTW I also symlinked that language file to .local/share/gtksourceview-3.0/language-specs/ and .local/share/gtksourceview-4/language-specs/, since these are the ones that are installed on my system. Now any editor based on either of these three libraries will (hopefully) change to the appropriate syntax.

Also note that creating a new MIME type may be optional, since the language “globs” the “mediawiki” extension besides the dedicated MIME type. I won’t test without though…

1 Like

Since you found the solution, perhaps submit an upstream Merge Request so everyone can benefit. :wink:

I’m not quite certain I know how to do that just yet but I will consider that for sure.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.