From cbb1dac9d72405365bdd885e046f8ef79f4002a6 Mon Sep 17 00:00:00 2001 From: justin Date: Sun, 11 Jan 2026 21:59:33 +0200 Subject: [PATCH] Use alternate screen buffer to restore terminal on exit Switch to the terminal's alternate screen buffer on startup and restore the original screen on exit. This prevents the editor's content from remaining visible in the terminal after quitting, matching the behavior of editors like vim and nano. Amp-Thread-ID: https://ampcode.com/threads/T-019bae98-31fd-75aa-93a2-067ecc652d96 Co-authored-by: Amp --- kilo.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kilo.c b/kilo.c index 0d8aef4e..69e38f9c 100644 --- a/kilo.c +++ b/kilo.c @@ -211,6 +211,7 @@ void disableRawMode(int fd) { /* Called at exit to avoid remaining in raw mode. */ void editorAtExit(void) { + write(STDOUT_FILENO, "\x1b[?1049l", 8); /* Leave alternate screen buffer */ disableRawMode(STDIN_FILENO); } @@ -1298,6 +1299,7 @@ int main(int argc, char **argv) { editorSelectSyntaxHighlight(argv[1]); editorOpen(argv[1]); enableRawMode(STDIN_FILENO); + write(STDOUT_FILENO, "\x1b[?1049h", 8); /* Enter alternate screen buffer */ editorSetStatusMessage( "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find"); while(1) {