I want to define an application for opening files based on the filename.
For example I want a file called Gemfile
or Dockerfile
to be opened with sublime or atom.
I know I can use the mime-type but for both files it will be text/plain
.
Or is there a mime-type for Gemfile
?
Best Answer
Matching based on filename is one of the easiest ways to define a mimetype. There is no official mimetype for Gemfiles or Dockerfiles, but take a look in
/usr/share/mime/packages/
and you will find many unofficial mimetypes that have<sub-class-of type="text/plain"/>
and are typically namedtext/x-*
, such astext/x-python
ortext/x-markdown
.Adding custom mimetypes is generally straightforward, especially in this case where we can just use the filename.
As you observed the current mimetype of a
Dockerfile
andGemfile
are bothtext/plain
:We will create a new mimetype
text/x-dockerfile
for files namedDockerfile
or*.dockerfile
and a new mimetypetext/x-gemfile
for files namedGemfile
. To accomplish this, create an XML file namedx-dockerfile.xml
with these contents:and another file named
x-gemfile.xml
with these contents:Next, install the mimetypes. For a local user, run these commands:
To install system-wide, run these commands:
Observe that the detected mimetypes have now changed.
Now you can assign these to whichever text editor you prefer.