23

I actually want to change the default text editor on my Ubuntu 12.04 from nano to Geany. When I used this code:

update-alternatives --config editor

.. I don't see Geany in the list.

So to add Geany, this is supposed to work right?

update-alternatives --install /usr/bin/geany geany /usr/bin/geany 10

Also, on a side note, can you tell me if you would personally suggest me to change the default editor from nano to Geany, and why?

  • @izx Absolutely :) I'd still love to know what was the command-line way of doing it though. Regardless, I'l mark your answer as accepted :) – Programming Noob Sep 30 '12 at 09:06

4 Answers4

18

The command line way is:

sudo update-alternatives --install /usr/bin/editor editor /usr/bin/geany 10

and then use sudo update-alternatives --config editor to select Geany if necessary.

As a graphical alternative to the command-line method, consider using Gnome Alternatives (sudo apt-get install galternatives). Run GAlternatives, select editor in the left column, and then add/choose Geany on the right.

ish
  • 139,926
  • 5
    I did that and Geany is selected * 3 /usr/bin/geany 10 manual mode but when I try to open a text file by double-clicking it in Nautilus, it still opens with gedit. – To Do Feb 18 '13 at 16:23
  • @ToDo Me too. Wonder how do I can undo those commands. –  Dec 30 '13 at 15:59
  • I used this: sudo update-alternatives --install /usr/bin/gnome-text-editor gnome-text-editor /usr/bin/geany 100 – VeganEye Aug 27 '20 at 22:37
16

As an alternative, this also worked for me:

xdg-mime default geany.desktop $(grep MimeType /usr/share/applications/geany.desktop | sed 's/MimeType=//' | sed 's/;/ /g')

This line fetches all MimeTypes gedit is registered for, performs some cleanup (the sed commands) and finally sets geany as default application for them.

I found it here along with a bunch of useful info on colour schemes.

Eliah Kagan
  • 117,780
Steve
  • 321
  • 2
    This worked like a charm. – Faisal Sayed Jul 19 '13 at 04:41
  • 1
    Awesome! Unlike the accepted answer, your solution actually works for me. Thanks! #12.04 – king_julien Sep 09 '13 at 14:40
  • Any idea how I can undo this? – king_julien Sep 12 '13 at 08:35
  • No idea, but look at the answer below because (the other) Steve has done a great job of explaining what this one actually does - you should be able to reverse engineer the effects from there. Why are you undoing it? Its been a great solution for me and has worked perfectly for me for over 6 months... – Steve Sep 17 '13 at 05:36
14

This is a little late but I thought it may help someone.

My problem is I don't like to copy and paste things I don't understand, so here's an explanation of the answers so far.

First and foremost, there are two issues here and both answers are, in there own right, correct:

  1. the default command-line editor - solved by 'izx'
  2. the file-type (mime-type) association in the desktop session for GUI applications - solved by 'Steve'.

Here in a little more detail:

  1. command line solution -from 'izx'

    • adds geany (a GUI application) to the alternative editor list and sets it as default.
    • I always thought that editor was usually reserved for text-based editors like vi, vim, nano, etc, but I have just checked and this seems to work as well.
    • the result means you can your chosen default will be used to open the file when for example:
  2. mime-type solution - from 'Steve'

At file level, there are two lists of importance:

/usr/share/applications/defaults.list - the system defaults list.

~/.local/share/applications/mimeapps.list - the users personal desktop session defaults list

What does the command do?

From the xdg-mime man page (man xdg-mime)

xdg-mime - command line tool for querying information about file type handling and adding descriptions for new file type

The xdg-mime default... command adds entries to the users personal list.

Let's break the command down.

Command:

xdg-mime default geany.desktop $(grep MimeType /usr/share/applications/geany.desktop | sed 's/MimeType=//' | sed 's/;/ /g')

The command can be read as:

set geany.desktop as the default application for the mime-type outputted by $(grep MimeType /usr/share/applications/geany.desktop | sed 's/MimeType=//' | sed 's/;/ /g')

Looking at

$(grep MimeType /usr/share/applications/geany.desktop | sed 's/MimeType=//' | sed 's/;/ /g')
  • takes all mime-types from the geany.desktop file
  • removes the "MimeType="
  • replaces ";" with " "
  • giving us:
    text/plain text/x-chdr text/x-csrc text/x-c++hdr text/x-c++src text/x-java text/x-dsrc text/x-pascal text/x-perl text/x-python application/x-php application/x-httpd-php3 application/x-httpd-php4 application/x-httpd-php5 application/xml text/html text/css text/x-sql text/x-diff

Looking at the users personal file after running the command, we can see all the file-associations set for the geany.desktop application:

[Default Applications]
x-scheme-handler/mailto=userapp-Thunderbird-ZP00XW.desktop
message/rfc822=userapp-Thunderbird-ZP00XW.desktop
application/x-extension-eml=userapp-Thunderbird-ZP00XW.desktop
application/x-perl=sublime_text.desktop
text/plain=geany.desktop
text/x-chdr=geany.desktop
text/x-csrc=geany.desktop
text/x-dtd=sublime_text.desktop
text/x-java=geany.desktop
text/mathml=sublime_text.desktop
text/x-python=geany.desktop
text/x-sql=geany.desktop
text/x-c++hdr=geany.desktop
text/x-c++src=geany.desktop
text/x-dsrc=geany.desktop
text/x-pascal=geany.desktop
text/x-perl=geany.desktop
application/x-php=geany.desktop
application/x-httpd-php3=geany.desktop
application/x-httpd-php4=geany.desktop
application/x-httpd-php5=geany.desktop
application/xml=geany.desktop
text/html=geany.desktop
text/css=geany.desktop
text/x-diff=geany.desktop

Knowing this, here is my particular problem and how I solved it.

Problem:

I wanted to changed the default "file opening" application in Nautilus from "gedit" -> "sublime text".

Solution:

Find the system defaults for gedit with:

$less /usr/share/applications/defaults.list | grep gedit
application/x-perl=gedit.desktop
text/plain=gedit.desktop
text/x-chdr=gedit.desktop
text/x-csrc=gedit.desktop
text/x-dtd=gedit.desktop
text/x-java=gedit.desktop
text/mathml=gedit.desktop
text/x-python=gedit.desktop
text/x-sql=gedit.desktop

Replace "gedit" with "sublime_text" and add the entries to the "Default Applications" section of my personal defaults.list

application/x-perl=sublime_text.desktop
text/plain=sublime_text.desktop
text/x-chdr=sublime_text.desktop
text/x-csrc=sublime_text.desktop
text/x-dtd=sublime_text.desktop
text/x-java=sublime_text.desktop
text/mathml=sublime_text.desktop
text/x-python=sublime_text.desktop
text/x-sql=sublime_text.desktop
muru
  • 197,895
  • 55
  • 485
  • 740
Steve-B
  • 251
  • 2
  • 4
  • This is very helpful but you can also consider editing and improving existing answers, too. It's often much less work. – Tom Brossman Jul 24 '13 at 09:41
  • 2
    Good point Tom, thanks. I'm always a little apprehensive about editing peoples answers. I will add a comment to the accepted answer. – Steve-B Jul 24 '13 at 13:11
  • Don't worry about editing someone else's post here, it is expected. Edits get peer-reviewed until you hit 2000 rep, then they go straight through. Keeping all the good info in one answer makes it easy to find later. – Tom Brossman Jul 24 '13 at 16:44
1

Because there are comments indicating that the accepted answer was not correct I feel I should point out this question can not have a single answer. If the question was changed to specify the CLI and the queries for the GUI moved to a new thread this would clear up the confusion. Too late for that I guess.

Anyway for nautilus (aka files) on gnome under Ubuntu 14.04. And given the accepted answer, you might (like I did) expect the equivalent would be:
donotdothisssudo update-alternatives --install /etc/alternatives/gnome-text-editor gnome-text-editor /usr/bin/geany 10

That's what I thought and it broke the links, so don't do that! If you did, this is what they were for me:
lrwxrwxrwx /usr/bin/gnome-text-editor -> /etc/alternatives/gnome-text-editor lrwxrwxrwx /etc/alternatives/gnome-text-editor -> /usr/bin/gedit

While repairing this error I recreated these links as follows:
lrwxrwxrwx /usr/bin/gnome-text-editor -> /etc/alternatives/gnome-text-editor lrwxrwxrwx /etc/alternatives/gnome-text-editor -> /usr/bin/geany

As all I have really done is to replace the secondary simlink, and I have desired response in my system my guess is that the task is simply this:
sudo ln -s /usr/bin/geany /etc/alternatives/gnome-text-editor

However running a CLI command to change something which is GUI specific, must be wrong. The place where my hands take me when searching for this change option is the nautilus menu system, next to the individual file type setting. However the only reason that I go there is because that is where part of the solution is. In my view if you have followed the logic of DPKG (update-alternatives), and included it in your distribution then you do not want a user space application like Nautilus interfering with your settings manager app. My humble suggestion to fix this would be to add a new editor settings control to settings >> details >> default applications, which would need to be a new page from the users point of view. The Nautilus team would then need to change its menu so that the user is redirected to that new page. This would compromise where my hands have taken me to look for the setting, where my hands 'should' have taken me instead and manages a single point entry process into a system setting. It looks like this change would need to be within the port of debian's galternatives, although I am just guessing. Of course there would be no point charging ahead with that kind of thing without the Nautilus team on board with the idea, they would also need to specify the API or equivalent mechanism.

The settings page in my view would need to cover both the default gnome editor selection and the application used to open each individual file (which is to replace the current functionality provided by Nautilus).

I post this as a passing thought, feel free to drive it forwards if it appeals to you. Sadly I don't know the procedure and the protocol necessary to do so myself.