This is a light version of the Vim editor for educational purposes. It implements the basic functionalities of Vim. Now it only support Linux and macOS platforms.
Usage: ./alayavim <file> [<file> ...]
- Normal mode
- ⬅️⬇️⬆️➡️ to move the cursor
BACKSPACEto move to the previous characterENTERto move to the next lineito enter insert mode:to enter command modeh,j,k,lto move left, down, up, and right respectively0to move to the beginning of the line$to move to the end of the lineGto move to the end of the fileggto move to the beginning of the fileddto delete the current lineyyto copy the current linepto paste the copied lineuto undo the last operationctrl + rto redo the last operation
- Insert mode
ESCto switch to normal mode
- Command mode
:qto quit (:q!to force quit):wto save:wqto save and quit:wato save all files:fileto display the file information:nor:nextto go to the next file (add!to override):por:prevto go to the previous file (add!to override):firstto go to the first file:lastto go to the last file:set numberto display line numbers:set nonumberto hide line numbers:s/old/new/gto replaceoldwithnewin the current line:%s/old/new/gto replaceoldwithnewin the current file:<number>to go to the line number
mkdir -p build/
cd build/
cmake ..
make -j
./alayavim <file> [<file> ...] main.cppis the program entry.core.cppimplementsCoreclass, which serves as a centralized controller to send commands to different file managersfilemanager.cppcontains theFileManagerclass to manage file contents and the corresponding cursor position. It controls the terminal display too.log.hcontainsLogclass to record the operations for undo and redo.utility.cppcontains utility functions (e.g., ANSI).
- Support editor-like display style in terminal
- Forbid buffer (i.e., must press "enter" to complete input) and echo (the character you pressed occurs in the terminal) to capture the key press. It is in
<unistd.h>, a POSIX (Portable Operating System Interface) header file.
void config_set(struct termios &oldt, struct termios &newt) { tcgetattr(STDIN_FILENO, &oldt); // terminal configuration newt = oldt; newt.c_lflag &= ~ICANON; // buffer forbidden newt.c_lflag &= ~ECHO; // echo forbidden tcsetattr(STDIN_FILENO, TCSANOW, &newt); // new configuration }
-
Distinguish the ESC key (
^) from the arrow keys (^[A,^[B,^[C,^[D): switch to nonblocking mode temporarily to read two more characters.int switch_to_nonblock(int fd) { int flags = fcntl(fd, F_GETFL, 0); if (flags == -1) { perror("fcntl"); return -1; } flags |= O_NONBLOCK; if (fcntl(fd, F_SETFL, flags) == -1) { perror("fcntl"); return -1; } return 0; }
-
ANSI escape sequences are a set of control codes used to control the formatting, color, and behavior of text in command-line interfaces. See
utility.hfor detailed examples.
- Forbid buffer (i.e., must press "enter" to complete input) and echo (the character you pressed occurs in the terminal) to capture the key press. It is in
dddcombo should not delete the lines twice.- Replace
- The cursor should move to a reasonable position.
- If the cursor is within one of the replaced patterns, it moves to the start of the new word.
- If the cursor is beyond the end of this line, it moves to the end of the line.
- Otherwise, it sticks to the adjacent word, instead of staying in the original position.
- The cursor should move to a reasonable position.
- Undo and Redo
- If two adjacent operations are done within 500ms, they are considered as a single operation in undo and redo.
- The cursor will move to the original position and the view adjusts accordingly.