How to associate text file type with Emacs on Ubuntu

1. background

MS Windows users might want to see this link instead for how to make Emacs (emacsclient) the default text editor.

This will be about Ubuntu, the Linux distribution that comes with, I think, the biggest support group. Even if you are not interested in using Emacs on MS Windows, you should read the troubleshoot section in that linked article because it’s applicable to Ubuntu as well.

By default, text files on Ubuntu open in GEdit, which is an easy-to-use feature-packed editor. Normally, to change the default text editor, you would right click on a text file and then open the “Properties” dialog and from there you can change the default association. You can see for example “GNU Emacs 24” as an item in the “Open with” applications list. If you make that item the default editor, from then on, all text files (including elisp files and org-mode files) will be opened with Emacs, but each time you open a text file, a new instance of Emacs is launched, which is not what you want. What you really want is to reuse an existing Emacs instance, i.e., to reuse the current Emacs session. You might have heard of emacsclient which is born to serve exactly this purpose. But that’s not the end of the story because it turns out that you will want to always pass some command line options to emacsclient.

2. create a new desktop file

If you installed Emacs by installing “GNU Emacs editor (metapackage)” from Ubuntu Software Center like I did, you should be able to find its desktop file, the full path of which is something like:

/usr/share/applications/emacs24.desktop

Your mileage may vary; if you are reading this from future, the desktop file’s name may be emacs26.desktop instead. This desktop file corresponds to the item “GNU Emacs 24 ” in the “Open with” list. We need to make a new desktop file that will handle passing some fixed options to emacsclient. Before that, let’s analyze the contents of emacs24.desktop, which follows:

[Desktop Entry]
Version=1.0
Name=GNU Emacs 24
GenericName=Text Editor
Comment=View and edit files
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=/usr/bin/emacs24 %F
TryExec=emacs24
Icon=/usr/share/icons/hicolor/scalable/apps/emacs24.svg
Type=Application
Terminal=false
Categories=Utility;Development;TextEditor;
StartupWMClass=Emacs24

The line MimeType=text/english.... is basically saying that the application should be allowed to open text files and other text-like files. The Exec field is the command to call, sometimes with fixed arguments. The line TryExec=emacs24 is saying that GNU Emacs 24 should be considered as installed when emacs24 can be found through PATH. The line StartupWMClass=Emacs24 seems to be about classification of windows.

Open that desktop file with an editor, and copy its contents to a new desktop file, the full path of which could be something like:

~/.local/share/applications/emacsclient24orgedit.desktop

and then modify just two lines, like this:

[Desktop Entry]
Version=1.0
# # Name=GNU Emacs 24
Name=GNU Emacs Client 24 or GEdit
GenericName=Text Editor
Comment=View and edit files
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
# # Exec=/usr/bin/emacs24 %F
Exec=/usr/bin/emacsclient.emacs24 -c --no-wait --alternate-editor=gedit %F
TryExec=emacs24
Icon=/usr/share/icons/hicolor/scalable/apps/emacs24.svg
Type=Application
Terminal=false
Categories=Utility;Development;TextEditor;
StartupWMClass=Emacs24

Let me help you spot the differences; original lines are commented out with two number signs and the new lines are inserted just after the commented out lines. You can make these changes easily with GEdit (some might say the quickest way would be with VI). Save it and restart your Ubuntu machine. Now there should be a new item “GNU Emacs Client 24 or GEdit” in the “Open with” list. Make that item the default editor; you can easily figure out how to do that if you open the “Properties” dialog after right clicking on a text file. Make Emacs server start automatically whenever you start Emacs. That can be done by simply adding (server-start) to your Emacs init file. Restart Emacs.

Now whenever you open a text file, it will open in an existing instance of Emacs if any, otherwise it will open in GEdit.

Let’s take a look at the Exec field:

Exec=/usr/bin/emacsclient.emacs24 -c --no-wait --alternate-editor=gedit %F

I wrote /usr/bin/emacsclient.emacs24 there because running the command whereis emacsclient on my system told me of /usr/bin/emacsclient and /usr/bin/emacsclient.emacs24 and some others. The file /usr/bin/emacsclient is a symbolic link to a symbolic link to /usr/bin/emacsclient.emacs24. Some of you might want to write /usr/bin/emacsclient instead, in which case you should also change the Name field accordingly.

The option -c is there so that each text file is opened in a new Emacs frame (of an existing Emacs instance) rather than in an existing Emacs frame. You can leave out this option if you want.

The option --no-wait is there so that the emacsclient process quits itself as soon as it’s done the job of telling the Emacs instance to open the text file.

OK, we are done. Read that troubleshoot section.

Some day, you might hear about Emacs Daemon and --alternate-editor=emacs which you should try later, but not right now if you are a beginner. As for why not right now, see the relevant sections in the linked article.

This article is part of the Living with Emacs collection.

3. why use a desktop file

Creating a desktop file does more than just making something a default text editor, it also makes it easy for users to select different editors to open a file on the fly (by selecting from the “Open with” menu). You could also have two desktop files for one editor; for instance, you could have one that creates a new Emacs frame, and the other that does not.

4. On two environment variables EDITOR and VISUAL

These two environment variables are what some Unix tools use to choose which text editor to launch when needed. Git is such an example. This Ubuntu help page mentions what these environment variables are for and how to set them in general. If you are curious about why there are two and/or what is the difference between the two, reading this and this might be satisfying. (For how to set them on MS Windows, see Windows Environment Variables Tutorial.)

This entry was posted in Emacs and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s