@@ -954,17 +954,22 @@ mod cmd {
957− fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
957+ fn _write<P: AsRef<Path>>(
958− let (view, doc) = editor.current();
958+ view: &View,
961+ ) -> Result<(), anyhow::Error> {
959− if let Some(path) = args.get(0) {
964+ if let Some(path) = path {
960− if let Err(err) = doc.set_path(Path::new(path)) {
965+ if let Err(err) = doc.set_path(path.as_ref()) {
961− editor.set_error(format!("invalid filepath: {}", err));
966+ return Err(anyhow!("invalid filepath: {}", err));
965 if doc.path().is_none() {
969 if doc.path().is_none() {
966− editor.set_error("cannot write a buffer without a filename".to_string());
971+ "cannot write a buffer without a filename".to_string()
969 let autofmt = doc
974 let autofmt = doc
970 .language_config()
975 .language_config()
@@ -974,6 +979,14 @@ mod cmd {
974 doc.format(view.id); // TODO: merge into save
979 doc.format(view.id); // TODO: merge into save
976 tokio::spawn(doc.save());
981 tokio::spawn(doc.save());
985+ fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
986+ let (view, doc) = editor.current();
987+ if let Err(e) = _write(view, doc, args.first()) {
988+ editor.set_error(e.to_string());
979 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
992 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
@@ -1010,6 +1023,52 @@ mod cmd {
1010 doc.later(view.id, uk)
1023 doc.later(view.id, uk)
1026+ fn write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1027+ write(editor, args, event.clone());
1028+ quit(editor, &[], event);
1031+ fn force_write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1032+ write(editor, args, event.clone());
1033+ force_quit(editor, &[], event);
1036+ fn _write_all(editor: &mut Editor, args: &[&str], event: PromptEvent, quit: bool, force: bool) {
1040+ .map(|(view, _)| (view.id, view.doc))
1041+ .collect::<Vec<_>>();
1043+ for (view_id, doc_id) in ids {
1044+ if let Some(doc) = editor.documents.get_mut(doc_id) {
1045+ let view = editor.tree.get(view_id);
1046+ if let Err(e) = _write(view, doc, None::<&str>) {
1047+ editor.set_error(e.to_string());
1049+ editor.close(view_id, false);
1054+ editor.close(view_id, false);
1060+ fn write_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1061+ _write_all(editor, args, event, false, false)
1064+ fn write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1065+ _write_all(editor, args, event, true, false)
1068+ fn force_write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1069+ _write_all(editor, args, event, true, true)
1013 pub const COMMAND_LIST: &[Command] = &[
1072 pub const COMMAND_LIST: &[Command] = &[
1014 Command {
1073 Command {
1015 name: "quit",
1074 name: "quit",
@@ -1067,6 +1126,41 @@ mod cmd {
1067 fun: later,
1126 fun: later,
1068 completer: None,
1127 completer: None,
1130+ name: "write-quit",
1132+ doc: "Writes changes to disk and closes the current view. Accepts an optional path (:wq some/path.txt)",
1134+ completer: Some(completers::filename),
1137+ name: "write-quit!",
1138+ alias: Some("wq!"),
1139+ doc: "Writes changes to disk and closes the current view forcefully. Accepts an optional path (:wq! some/path.txt)",
1140+ fun: force_write_quit,
1141+ completer: Some(completers::filename),
1146+ doc: "Writes changes from all views to disk.",
1151+ name: "write-all-quit",
1152+ alias: Some("waq"),
1153+ doc: "Writes changes from all views to disk and close all views.",
1154+ fun: write_all_quit,
1158+ name: "write-all-quit!",
1159+ alias: Some("waq!"),
1160+ doc: "Writes changes from all views to disk and close all views forcefully (ignoring errors).",
1161+ fun: force_write_all_quit,
1072 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {
1166 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {