I am writing a terminal-based application, but I want the user to be able to edit certain text data in a separate editor. For example, if the user chooses to edit the list of current usernames, the list should open as a text file in the user’s favorite editor (vim, gedit, etc.). This will probably be an environment variable such as $MYAPPEDITOR. This is similar to the way commit messages work in svn.
Is the best way to do this to create a temporary file in /tmp, and read it in when the editor process is terminated? Or is there a better way to approach this problem?
There’s already a
$EDITORvariable, which is extremely standard and I have seen it working on a wide variety of unixes. Also,viis always an option on any flavor of unix.Debian has a
sensible-editorcommand that invokes$EDITORif it can, or falls back to some standard ones otherwise. Freedesktop.org has anxdg-opencommand that will detect which desktop environment is running and open the file with the associated application. As far as I know,sensible-editordoesn’t exist on other distributions, and of coursexdg-openwill fail in a text-only environment, but it couldn’t hurt to try as many options as possible, if you think it’s important that a desktop user can see their happy shinygeditorkateinstead of scary oldviornano. 😉The way
crontabandsudoeditwork is also by making a file in /tmp.gitputs it under .git, andsvnactually puts it in the current directory (not /tmp).