0

I used to use Sublime Text editor, now I plan to use Atom. It would be really nice to have some alias or variable called "editor" I might use in all scripts / commands / launchers and link this alias to an app that might be changed any time later without affecting those commands.

How could I do that? Thank you :)

3 Answers 3

3

The EDITOR and VISUAL variables are well-known variables commonly used for this purpose (cron, sudo, bash, etc. use it). Usually, VISUAL takes precedence over EDITOR. However, both are typically used in non-GUI environments. Nothing prevents either from being a GUI program, but they usually aren't.

You can usually respect the user's settings in an desktop environment by using xdg-open. xdg-open relies on various desktop-environment specific tools like gnome-open or kde-open (How does xdg-opendo its work?).

So, in scripts, just do:

xdg-open /some/file 
xdg-open proto://some/uri
1
  • Thank you, I am gonna learn more about xdg-open, sounds handy.
    – Sebastian
    Sep 20, 2015 at 21:04
3

There is already a generic binary called editor i.e. /usr/bin/editor provided by the Debian alternatives system (update-alternatives).

This binary is actually a symbolic link to /etc/alternatives/editor :

$ ls -l /usr/bin/editor 
lrwxrwxrwx 1 root root 24 Feb  9  2015 /usr/bin/editor -> /etc/alternatives/editor

which in turn is a symbolic link to the actual editor select based on priority or manually :

$ ls -l /etc/alternatives/editor
lrwxrwxrwx 1 root root 18 Feb 10  2015 /etc/alternatives/editor -> /usr/bin/vim.basic

Now if i open a file with :

editor ~/.bashrc

this would actually do :

vim.basic ~/.bashrc

Lets change the editor :

$ sudo update-alternatives --config editor 
There are 5 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/nedit       40        manual mode
* 4            /usr/bin/vim.basic   30        manual mode
  5            /usr/bin/vim.tiny    10        manual mode

The one with the * is the currently selected one, navigate to the one you like and select that by pressing Enter or directly use update-alternatives --set or you can set priority as you and see in the third column by update-alternatives --install command. To get information about something you can use update-alternatives --query or update-alternatives --list commands.

To install a new alternative use update-alternatives --install command. For example adding the editor /usr/bin/foobar to the editor alternative system and giving it a priority of 100 so that this would be the default now :

sudo update-alternatives --install editor /etc/alternatives/editor /usr/bin/foobar 100

Check man update-alternatives to get details.

2

You can try creating a soft link named editor which points to the current editor you are using. When you switch to some other editor you can simply point the soft link to your new editor. Command to create a soft link named editor to point to Sublime text 3:

ln -sf subl3 editor

Better create this soft link in either /usr/bin or /bin, otherwise add the directory containing link to your PATH that it is accessible from terminal and scripts.

1
  • This is exactly what I was looking for, thank you :-)
    – Sebastian
    Sep 20, 2015 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.