Function edit::edit

source ·
pub fn edit<S: AsRef<[u8]>>(text: S) -> Result<String>
Expand description

Open the contents of a string or buffer in the default editor.

This function saves its input to a temporary file and then opens the default editor to it. It waits for the editor to return, re-reads the (possibly changed/edited) temporary file, and then deletes it.

Arguments

text is written to the temporary file before invoking the editor. (The editor opens with the contents of text already in the file).

Returns

If successful, returns the edited string. If the edited version of the file can’t be decoded as UTF-8, returns ErrorKind::InvalidData. If no text editor could be found, returns ErrorKind::NotFound. Any errors related to spawning the editor process will also be passed through.

Examples found in repository?
examples/edit-example.rs (line 9)
1
2
3
4
5
6
7
8
9
10
11
fn main() {
    let string = format!(
        "Hello world!\nYou are editing this file in '{}'.",
        edit::get_editor()
            .expect("can't find an editor")
            .to_str()
            .unwrap()
    );
    let edited = edit::edit(string).expect("editing failed");
    println!("after editing:\n{}", edited);
}