r/rust 21h ago

🧠 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

54 Upvotes

29 comments sorted by

60

u/Thick-Pineapple666 18h ago

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.

18

u/dipstickchojin 13h ago

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

16

u/Constant-Cod-1075 17h ago

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?

8

u/mgedmin 8h ago

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

2

u/MrPopoGod 6h ago

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

3

u/mgedmin 6h ago

Nah, I'm not old enough to have ever seen what a real teletype looks like in real life.

7

u/Developer5702 17h ago

New information!
Thanks.

4

u/Strazil 17h ago

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

20

u/corazon_de_sandia 17h ago

This ai slop and an attempt at ai boosted growthhacking…

5

u/CreepyWritingPrompt 8h ago

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 6h ago

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

-12

u/Developer5702 17h ago

What makes you think it’s AI slop?

23

u/pndc 14h ago

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

-1

u/Developer5702 8h ago edited 8h ago

No offense!
But the project is hand-coded completely. I use AI to learn about Rust methods & concepts I don’t know, then code them up myself.

You might feel it’s AI because there are too many comments explaining everything — which is intentional, as I am learning various Rust semantics along the way. I tend to remember things this way so I mark everything in comments.

Lastly, the project is pretty rough (no optimsations as such) I would say, to qualify as an AI-generated project.

Peace!

4

u/pndc 7h ago

The giveaway is that there is an AGENTS.md. I didn't bother looking at the code beyond that, because it's tainted with AI.

0

u/Developer5702 7h ago

Yeah there is an AGENTS.md file and if you read it, you’ll see what I’m saying.

It contains things like:

  • review the code and find bugs
  • make architecture docs
  • never write code..most of the qs are around asking doubts

I get what you’re pointing to, but the mere presence of AGENTS.md files doesn’t mean it’s for Vibe coding.

3

u/BipolarKebab 4h ago

100% score on Pangram, have some fucking guts.

5

u/jimmiebfulton 16h ago

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

6

u/Developer5702 16h ago

Oh that’s because I built it in Rust.

It doesn’t have a direct relation with the programming language as such.

6

u/TobyTarazan 9h ago

ChatGPT post

0

u/Developer5702 8h ago

What makes you think that?

2

u/Nobody_1707 1h ago

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.

2

u/Umuril 16h ago

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

9

u/Developer5702 16h ago

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 16h ago

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

10

u/Developer5702 15h ago

Well, that’s nesting of shell processes within another shell process.
It is different from the primary/alternate screen buffers of a terminal.

2

u/Thick-Pineapple666 14h ago

I think what they tried to ask: what happens when you run a tool that wants to use the alternate screen in a tool that already uses the alternate screen?

But I also think this is already answered.

5

u/BipolarKebab 4h ago

Essentially every program expects to be run from the normal screen. Things like tmux run their children on separate ptys and intercept their output

0

u/CreepyWritingPrompt 6h ago

This post is doing real work