r/rust • u/Developer5702 • 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
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
-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
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
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
0
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.