The patch also includes a test. ZZ is in POSIX vi. (I sent this a week ago without subscription and it's probably in some moderation queue, sorry about that)
-- Alex Kapranoff.
From 5032003069e0458085a00da111c77c20d199ac95 Mon Sep 17 00:00:00 2001 From: Alex Kapranoff <[email protected]> Date: Sat, 15 Nov 2025 23:39:38 -0800 Subject: [PATCH] vi: Support ZZ to save and exit --- tests/vi.test | 2 ++ toys/pending/vi.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/vi.test b/tests/vi.test index 733feba8..bdebcb26 100644 --- a/tests/vi.test +++ b/tests/vi.test @@ -29,3 +29,5 @@ vitest "insert multiple times ascii" "iab\x1bicd\x1bief\x1b" "xyz" "acefdbxyz" vitest "insert multi yank move and push ascii" "ixyz\x1byyjp" \ "abc def\nghi jkl\nmno pqr\n" "xyzabc def\nghi jkl\nxyzabc def\nmno pqr\n" + +testcmd "ZZ save" "-s /dev/stdin input 1>/dev/null 2>/dev/null && cat input" "helloabc def" "abc def" "ihello\x1bZZ" diff --git a/toys/pending/vi.c b/toys/pending/vi.c index 19c8d11f..ab457753 100644 --- a/toys/pending/vi.c +++ b/toys/pending/vi.c @@ -66,7 +66,7 @@ GLOBALS( char *filename; int vi_mode, tabstop, list, cur_col, cur_row, scr_row, drawn_row, drawn_col, - count0, count1, vi_mov_flag; + count0, count1, vi_mov_flag, vi_exit; unsigned screen_height, screen_width; char vi_reg, *last_search; struct str_line { @@ -1228,6 +1228,15 @@ static int vi_find_prev(char reg, int count0, int count1) return 1; } +static int vi_ZZ(char reg, int count0, int count1) +{ + if (modified() && write_file(0) != 1) { + return 1; // Write failed, don't exit + } + TT.vi_exit = 1; + return 1; +} + //NOTES //vi-mode cmd syntax is //("[REG])[COUNT0]CMD[COUNT1](MOV) @@ -1252,6 +1261,7 @@ struct vi_special_param { {"I", &vi_I}, {"J", &vi_join}, {"O", &vi_O}, + {"ZZ", &vi_ZZ}, {"N", &vi_find_prev}, {"n", &vi_find_next}, {"o", &vi_o}, @@ -1848,6 +1858,8 @@ void vi_main(void) break; } } + // Check for exit flag (used by ZZ command) + if (TT.vi_exit) goto cleanup_vi; } cleanup_vi: linelist_unload(); -- 2.50.1
_______________________________________________ Toybox mailing list [email protected] http://lists.landley.net/listinfo.cgi/toybox-landley.net
