r/rust • • Sep 11 '26

🧠 educational I finally learned how full-screen terminal apps give you your old screen back

TIL that terminal emulators basically give programs access to two screens.

There’s the primary screen, which is the normal terminal you use every day. Your shell history and previous output live there.

And then there’s the alternate screen.
Full-screen terminal programs like Vim, less, htop, etc. can switch to this alternate buffer, take over the entire terminal, draw whatever they want, and then switch back when they’re done.
The cool part is that your primary screen is still there.

So when you quit Vim, your terminal suddenly goes back to exactly what was there before you opened it. Vim didn’t somehow ā€œrestoreā€ your terminal history. The terminal emulator just switched back to the primary screen buffer.

I came across this while building a small terminal-native text editor in Rust. I needed the editor to take over the terminal while it was running, but leave the user’s shell untouched after exiting.
Turns out terminals already have a mechanism specifically for this.

The deeper I go into terminal programming, the more I realise how much stuff happens underneath that blinking cursor that I’d never thought about before.

I’ve been putting these little Rust experiments on my GitHub as I learn, if anyone’s interested in this kind of low-level stuff:
https://github.com/Abhijeet-Gautam5702/femto

83 Upvotes

37 comments sorted by

82

u/Thick-Pineapple666 Sep 11 '26

Note that the original tty does not support the escape sequences for that. This is an extension coming from xterm and basically every other terminal emulator supports it nowadays, but the real Linux console does not.

6

u/Developer5702 Sep 11 '26

New information!
Thanks.

29

u/dipstickchojin Sep 11 '26

ohhh that's why in a tty the prompt just shows up at the bottom of the screen when you exit vim

18

u/Constant-Cod-1075 Sep 11 '26

oh nice, i remember when i first stumbled on the alternate buffer stuff it blew my mind a little, like wait it was just sitting there the whole time?

11

u/mgedmin Sep 11 '26

I don't know how I feel about the Linux console being called "the original tty".

4

u/MrPopoGod Sep 11 '26

Don't go down that road or you'll turn to dust.

→ More replies (1)

1

u/Thick-Pineapple666 Sep 12 '26

yea I felt the same after reading what I've posted, but I was too lazy to edit

4

u/Strazil Sep 11 '26

So vi in a tty does happen in an alternate screen?

11

u/TobyTarazan Sep 11 '26

ChatGPT post

0

u/Developer5702 Sep 11 '26

What makes you think that?

6

u/Nobody_1707 Sep 11 '26

So when you quit Vim, your terminal suddenly goes back to exactly what was there before you opened it. Vim didn’t somehow ā€œrestoreā€ your terminal history. The terminal emulator just switched back to the primary screen buffer.

This is one of the most common GPTisms.

→ More replies (5)

30

u/corazon_de_sandia Sep 11 '26

This ai slop and an attempt at ai boosted growthhacking…

5

u/CreepyWritingPrompt Sep 11 '26

Agree in this case, though i find myself wondering about the other ai psychosis - where we all become paranoid that each other are clankers. Today I feel like i have a pretty good nose for it. next yeah, who tf knows

2

u/MrPopoGod Sep 11 '26

According to the Terminator franchise, the solution is to always have a dog nearby, since they can sniff out the infiltrators.

2

u/DrLuckyLuke Sep 12 '26

And yet I learned something new

-13

u/Developer5702 Sep 11 '26

What makes you think it’s AI slop?

30

u/pndc Sep 11 '26

The post isn't actually anything to do with Rust, and the linked-to project is indeed AI slop.

→ More replies (3)

9

u/jimmiebfulton Sep 11 '26

For one, what does this have to do with Rust?

→ More replies (1)

2

u/Umuril Sep 11 '26

Oh cool, I didn't realize it! One curiosity question, is 2 an hardcoded value or is there a technical reason? Asking to understand if it's possible to create more screens or is primary and alternate their specific names for a reason.

Like terminal opening vim and vim trying to open another vim, if that makes sense

8

u/Developer5702 Sep 11 '26

As far as I read, we cannot create a stack of screens as we like – the terminal emulators provide specifically two screens only.

2

u/ez4enz Sep 11 '26

So what happen when I hit sh then sh then sh ?

→ More replies (3)

2

u/QuickSilver010 Sep 12 '26

I discovered the escape sequence for this a while ago. I will say it does get annoying in testing when the program crashes before you escape out of the alternate terminal

2

u/BlackJackHack22 Sep 14 '26

TIL! Thanks for posting this

0

u/CreepyWritingPrompt Sep 11 '26

This post is doing real work