The default text editor on a system is usually stored in the $EDITOR
environmental variable. For example, on my system I have:
$ echo $EDITOR
/usr/bin/emacs
So, you can simply run
$ $EDITOR test.txt
NOTE: This is not necessarily the same editor defined in the graphcial environment's settings. Use the method below to get that.
Alternatively, if the system is configured to use it, you can launch the default program associated with a mime type with xdg-open
(open
on OSX):
$ xdg-open test.txt
But that will not hold the terminal until the command closes. However, you can use the mime setup to find out what program would be opened and then call the program yourself. To do this, get the program associated with the text mime type:
$ xdg-mime query default text/plain
pluma.desktop;sublime_text.desktop
So, now you can parse that line to get the program name:
editor=$(xdg-mime query default text/plain | sed 's/\..*//')
$editor test.txt
NOTE: This assumes that the .desktop
file has the name of the actual executable. A safer way might be to locate the desktop file itself and grep the executable from it. Then, launch the program you found. You can do the whole thing with this command:
editor=$(grep -i ^exec $(locate -n 1 $(xdg-mime query default text/plain |
cut -d';' -f 1)) |
perl -pe 's/.*=(\S+).*/$1/')
$editor test.txt
kate test.txt
this is the default behavior. – terdon♦ Feb 8 '14 at 17:14system()
(man 3 system
. Your program will continue after your editor exits. If you want to detect just saving you need to do something more complicated and look atpopen()
and observe the saving of the file programmatically (i.e. the program cannot just wait). BTW. This belongs on Stack Overflow. – Anthon Feb 13 '14 at 5:35