Live data from Hacker News

Entering text in the terminal is complicated

jvns.ca

21–30 of 229 posts

Re: Entering text in the terminal is complicated

#21
> First, there’s “the baseline” – what happens if a program just accepts text by calling fgets() or whatever and doing absolutely nothing else to provide a nicer experience.

[...] there are actually a few features that you get for free just from your terminal, without the program needing to do anything special at all.

The things you get for free are: [...]

- backspace

- Ctrl+W, to delete the previous word

- Ctrl+U, to delete the whole line

Linux noob here, so this may be a stupid question, but how does that work under the hood?

The documentation for fgets() says [1]:

> Reads at most count - 1 characters from the given file stream and stores them in the character array pointed to by str. Parsing stops if a newline character is found (in which case str will contain that newline character) or if end-of-file occurs.

Does that mean that, by default, fgets() blocks until the user enters a newline and, before they do, it lets them edit the line buffer using the backspace, CTRL+W and CTRL+U shortcuts?

But there seems to be no guarantee that a program is using fgets() to read an entire line - it could also set count to 2 and read individual characters. Those could not be "unread" anymore when a backspace occurs. Is there some magic that lets fgets() buffer characters internally in such a situation, or would the backspace/line editing functionality just be broken then?

[1] https://en.cppreference.com/w/c/io/fgets

Re: Entering text in the terminal is complicated

#22

I have continually contented that the terminal is the single thing eternally condemning Linux to It's not just entering text. The entire experience is complicated. It's a Concorde jet cockpit[1] (with no labels too), when 95% of the population just wants to fly their drone around[2]. [1] https://qph.cf2.quoracdn.net/main-qimg-2566f4c91b894e4169d77... [2] https://media.thedroningcompany.com/images/tincy/WQZpC56vqMp...

Doesn't Windows, the most popular OS out there, have a cmd.exe terminal thing? Why isn't that harming Windows?

The Windows Command Prompt's default experience is worse than most if not all terminals you find on Linux systems. Even in play TTY, you're bound to find that the shortcuts the author mentioned work like a charm.

To make my cmd.exe bearable I am using https://chrisant996.github.io/clink

tl;dr You're comparing the choice of wheels on a plane to what makes planes sell more.

Re: Entering text in the terminal is complicated

#25
post #21

> First, there’s “the baseline” – what happens if a program just accepts text by calling fgets() or whatever and doing absolutely nothing else to provide a nicer experience. [...] there are actually a few features that you get for free just from your terminal, without the program needing to do anything special at all. The things you get for free are: [...] - backspace - Ctrl+W, to delete the previous word - Ctrl+U, t…

It's provided by the terminal (emulator) in "cooked" mode: https://en.wikipedia.org/wiki/Terminal_mode

raw mode doesn't do this.

Re: Entering text in the terminal is complicated

#26
I have great respect for Julia Evans and love her contributions that get shown here.

But...

> "I’ve always thought that vi mode seems really cool, but for some reason even though I’m a vim user I didn’t really like using it when I tried it."

Really, that seems weird to me.

I use zsh (btw) and the problem she describes is a non-issue. And I am not a pro. (Sorry, Julia).

Re: Entering text in the terminal is complicated

#27
post #21

> First, there’s “the baseline” – what happens if a program just accepts text by calling fgets() or whatever and doing absolutely nothing else to provide a nicer experience. [...] there are actually a few features that you get for free just from your terminal, without the program needing to do anything special at all. The things you get for free are: [...] - backspace - Ctrl+W, to delete the previous word - Ctrl+U, t…

It's provided by the terminal (emulator) in "cooked" mode: https://en.wikipedia.org/wiki/Terminal_mode raw mode doesn't do this.

Ah, that's exactly the answer and makes a lot of sense. Thanks a lot.

Re: Entering text in the terminal is complicated

#28
post #5

It took the author 15 years to learn basic readline commands?

Yeah, I have my doubts about the claim. I forgot what I read way back when as an intro book, but I suspect it was more like a month in my case, if that. But I remember having been surprised at various points that there was an undo (C-_), and a clipboard (C-y to paste) including a history (M-y). So yeah, I guess it depends on the scope. Maybe it's a hyperbole? Dunno, doesn't really read that way.

How can you discover something on the terminal itself? You fall into a pattern and stick with it. It is only if you get out of band information that you can learn these tricks.

Re: Entering text in the terminal is complicated

#29
post #18

> took me maybe 15 years of using the terminal every single day to get used to using Ctrl+A to go to the beginning of the line (or Ctrl+E for the end). And the curse of bad defaults strikes again (daily). After the realization that this is a weird default you don't train yourself for 15 years, but change it to your comfortable/common keybinds!

I wonder what terminal that is, because the Home and End keys, which I'm pretty sure are the expected alternatives, work fine in every terminal for me on Linux.
Post reply on HN