When we write the command vi <filename>
what exactly happens - there is no new window being opened for it, it seems that it certainly enters into some other mode out of the shell. And when we get out of vi
we return back to the shell.
Does it use hashing to check whether the <filename>
is already present or not and if:
create
system call to reserve space on disk.open
.Apart from this does it happens with all the text editors like nano
& emacs
?
vi
uses the terminal (actually, terminal emulators). It is a tty, in Unix parlance. Read The TTY Demystified, tty(4) (for /dev/tty
), pty(7) (pseudoterminals).
It uses some terminal library like 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.
That is not a mode of the shell, but of the tty (dealt with the terminal emulator). ANSI escapes codes are also relevant.
See also stty(1).
Terminal IO is not only buffered in the libc, but also in the kernel. Read about line discipline.
Regarding editing a file, vi
works like most 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)) only when you save the buffer.
BTW, vim
is free software (or open source), so download and study its source code of vim
. And emacs and nano are also free software.
Take time to read some good Linux system programming book (like the old 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)
BTW, in 2023, consider using a graphical user interface (e.g. Xorg or Wayland display servers) with open source editors using some open source graphical widget toolkit. Recent variants of vim
or GNU emacs etc enable you to use the mouse and various fonts and syntax highlighting.