Function edit::get_editor
source · pub fn get_editor() -> Result<PathBuf>
Expand description
Find the system default editor, if there is one.
This function checks several sources to find an editor binary (in order of precedence):
- the
VISUAL
environment variable - the
EDITOR
environment variable - hardcoded lists of common CLI editors on MacOS/Unix
- hardcoded lists of GUI editors on Windows/MacOS/Unix
- platform-specific generic “file openers” (e.g.
xdg-open
on Linux andopen
on MacOS)
Also, it doesn’t blindly return whatever is in an environment variable. If a specified editor
can’t be found or isn’t marked as executable (the executable bit is checked when the default
feature better-path
is enabled), this function will fall back to the next one that is.
Returns
If successful, returns the name of the system default editor.
Note that in most cases the full path of the editor isn’t returned; what is guaranteed is the
return value being suitable as the program name for e.g. Command::new
.
On some platforms, a text editor is installed by default, so the chances of a failure are low
save for PATH
being unset or something weird like that. However, it is possible for one not
to be located, and in that case get_editor
will return ErrorKind::NotFound
.
Example
use edit::get_editor;
// will print e.g. "default editor: nano"
println!("default editor:", get_editor().expect("can't find an editor").to_str());