How to change default program for files ending in one extension
This answer is adapted from Ondra Žižka's answer here: How do I set the default program?. Please upvote there as well if this helps you.
To change the default program for files with a specific file type, a mime type needs to be created for each file extension. This is because Ubuntu tracks this indirectly:
- file extension -> MIME types
- MIME type -> application(s) to open
- Applications -> list of compatible MIME types
For Ubuntu (18.x), the following applies:
file extension -> MIME types
This is determined from files in /usr/share/mime/packages/
.
E.g.:
cat << EOF | sudo tee /usr/share/mime/packages/staruml.xml
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="application/staruml-project">
<comment>StarUML project</comment>
<glob pattern="*.MDJ"/>
<glob pattern="*.mdj"/>
</mime-type>
</mime-info>
EOF
Other xml files are generated from these and placed in /usr/share/mime/
. Sometimes these files may be found online, especially for open source software packaged for Ubuntu. For example, the mime files for KiCad can be found in the KiCad source code repository: https://gitlab.com/kicad/code/kicad/-/tree/master/resources/linux/mime
MIME type -> application(s) to open
This is driven by ~/.config/mimeapps.list
or possibly ~/.local/share/applications/mimeapps.list
. This should override /usr/share/applications/mimeapps.list
. For example:
echo 'application/staruml=staruml.desktop' | tee -a ~/.local/share/applications/mimeapps.list
Or, to change for all users:
echo 'application/staruml=staruml.desktop' | sudo tee -a /usr/share/applications/mimeapps.list
Applications -> list of compatible MIME types
This is driven by the .desktop
files in /usr/share/applications/
. That file can contain a MimeType=
entry with semicolon-separated list of MIME types. For instance, the EOG app has MimeType=image/bmp;image/gif;image/jpeg;image/jpg;...
.
cat << EOF | sudo tee /usr/share/applications/staruml.desktop
[Desktop Entry]
Name=StarUML
Comment=StarUML
GenericName=StarUML
Exec=/sw/prog/StarUML/StarUML-3.1.0-x86_64.AppImage %U
Icon=/sw/prog/StarUML/staruml.png
Type=Application
StartupNotify=false
Categories=Development;Programming;Modeling;UML
MimeType=application/staruml-project;
EOF
This makes it a full circle. The changes take effect after running
sudo update-mime-database /usr/share/mime
and, for icons,
sudo gtk-update-icon-cache /usr/share/icons/gnome -f
Or, you may need to logout/login.
Then, mimetypes for a given file extension can be checked using mimetype
:
$ mimetype .mdj
.mdj: application/staruml-project
References:
- How do I set the default program?
- https://coderwall.com/p/qjda2q/create-new-mime-type-and-assign-an-icon-to-it-in-ubuntu
- https://help.ubuntu.com/community/AddingMimeTypes
- How to assign (set) a MIME type to a file?
Credit: Ondra Žižka (Adapted)