当我们在 shell 上打开 Vi 编辑器时到底发生了什么

What exactly happens when we open a Vi editor on a shell

当我们写命令 vi <filename> 到底发生了什么 - 没有新的 window 被打开,似乎它确实进入了 [=24= 之外的其他模式]shell。当我们离开 vi 时,我们 return 回到 shell。

它是否使用散列来检查 <filename> 是否已经存在以及是否:

除此之外,所有文本编辑器都会发生这种情况,例如 nano & emacs?

vi 使用终端(实际上,terminal emulators). It is a tty, in Unix parlance. Read The TTY Demystified, tty(4) (for /dev/tty), pty(7) (pseudoterminals)。

它使用一些终端库,例如 ncurses, built above termios(3). See also ioctl_tty(2) (actually vim don't use ncurses, just the lower level libtinfo related to ncurses)

it seems that it certainly enters into some other mode out of the shell.

这不是 shell 的模式,而是 tty 的模式(处理终端仿真器)。 ANSI escapes codes 也相关。

另见 stty(1)

终端IO不仅在libc中有缓冲,在内核中也有缓冲。了解 line discipline.

关于编辑文件,vi 与大多数 text editors, it maintains a data structure in memory describing the current content of a buffer and work on that structure in memory. The content is written to the file (using file related system calls like open(2), write(2), close(2)) 一样,仅当您保存缓冲区时。

顺便说一句,vim is free software (or open source), so download and study its source code of vim. And emacs and nano也是免费软件。

花时间阅读一些不错的Linux系统编程书籍(比如旧的ALP or something newer) then syscalls(2). Notice that terminal IO is quite complex in the details (because terminals such as VT100 or VT220 were complex). So you probably want to use a library like ncurses (or perhaps readline