Using the Debian alternatives system
Posted by Steve on Fri 11 Feb 2005 at 22:40
Debian is the single largest GNU/Linux distribution around, with more developers, more supported platforms and more packages than any other. With so many choices it's often the case that people will have differing ideas about the tools to use - such as text editors. The Debian alternatives system attempts to solve this problem.
There are many examples of a program having to invoke another one by name - for example many programs will need to allow the user to make changes to a file. So they must run a text editor, but which one? It's not appropriate for Debian to mandate the use of a particular tool to do a particular job, if there are several alternatives available.
For example when scheduling commands with Cron you will be presented with an editor to make changes to the crontab file.
Rather than the cron package forcing the use of a particular editor it will allow you to specify the editor to use via the environmental variable EDITOR. Using environmental variables like this is fairly common in the Unix world, as it allows each program to offer the user a choice.
However Debian has a different solution to the problem.
For some special classes of applications which have many programs that can be used, Debian allows you to choose which particular alternative you wish to be used, globally. This covers a set of programs such as:
- Text editors
- Web browser
- Window managers
The way it works is to have a set of standard command names such as editor, www-browser, and view. Each of those commands are merely symobilic links to the real command.
This is a symbolic link which links to:
This in turn links to the real editor, either vim or emacs in our example.
This extra level of indirection allows you to easily update the system to execute the editor you prefer. You could update the symbolic links by hand, but the preferred approach is to run:
update-alternatives --set editor /usr/bin/emacs21
This will set the symbolic link in the directory /etc/alternatives to point to the emacs binary - so now whenever a command runs "editor" emacs will be invoked.
If you wish to change your mind, and use vim in the future you merely need to run:
update-alternatives --set editor /usr/bin/vim
This will cause vim to be executed whenever you run "editor".
The command has several useful options which you can find discussed in the manpage, view that by running:
For example you can see which "real" program will be executed for a given name:
skx@lappy:~$ update-alternatives --list x-www-browser /usr/bin/mozilla-firefox
This shows us that executing the command "x-www-browser" will end up executing mozilla-firefox, on your machine something completely different could be executed!