For a course, I am using the Racket language, which bundles its own IDE (Dr Racket) with the install. I successfully installed it on my Ubuntu 22.04 machine.
However, the installer does not create a mimetype for .rkt
files (the default extension for Racket code files used by the IDE). I wanted my .rkt
files to open in the Racket IDE when I click on them, so I created a custom mimetype, called text/rkt
, which worked fine. The custom icon works and when I double click the files they open in the Racket IDE. On code files I've already created with code inside them, the custom mimetype seems to work correctly.
However, if I do touch test.rkt
for example to create a new file, this file gets assigned the text/plain
mimetype and when I double click it, it opens the text editor instead of the Racket IDE. This is not ideal, because I would like to be able to create empty files from the terminal and open them in the IDE (it's more convenient than doing Save As -> whatever every time in the IDE).
Is it possible to have my empty files with the *.rkt
extension be recognized as my custom mimetype, instead of as plain text?
Here's the code for my custom mime type in case I did something wrong:
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="text/rkt">
<comment>Racket code file</comment>
<glob pattern="*.rkt"/>
</mime-type>
</mime-info>
I followed the Ubuntu documentation here which I can see was last edited in 2014, as well as a post here which is more recent but maybe something changed since then. I'm new to linux so maybe I don't know where to look to find the correct instructions.
Actually, it gets assigned the
application/x-zerosize
MIME-type, whose default handler is usually the default text editor.Quoting Emmanuele Bassi, from the GNOME team:
Which means that if the file exists and is on a local storage, we won't do it like Windows (guessing based on the extension), but we'll look at magic numbers. For a 0-length file, looking at magic numbers will result in
application/x-zerosize
:This however isn't the case for other distributions / desktop environments. On my OpenSuse Leap / KDE box:
So there could be a way to make Ubuntu / GNOME work like OpenSuse Leap / KDE. I tried researching this, but so far I haven't understood neither how this could be done nor whether this would require changing source code somewhere.
However, for the purpose of having "empty" .rkt files opening with Dr Racket after
touch
ing them, since non-0-length .rkt files are opened by Dr Racket already, you could overridetouch
's behavior by having it create a .rkt file containing just a comment in case the command follows the synopsystouch <filename_ending_in_.rkt>
and<filename_ending_in_.rkt>
doesn't exist, by defining a Bash function such as this one (which will defer to the actualtouch
if fed more than one argument / the argument doesn't end in .rkt / a directory, file etc with the same name as the argument exists already):