A minimal terminal text viewer/editor written in C++ with ncurses. It opens a file, renders it line-by-line, and lets you move a cursor and run simple : commands (:q, :i). This is a learning project focused on terminal UI, file I/O, and keystroke handling.
- Open and display a text file line-by-line.
- Arrow-key cursor movement on the rendered text.
:commands::q— quit viewer:i— enter insert/navigation routine (work-in-progress)
- Handles Windows CRLF files by stripping
\rso lines don’t overwrite.
- ncurses rendering: You update a virtual screen;
refresh()pushes it to the terminal. Cursor moves withmove(y, x)(origin top-left(0,0),y= row,x= column). - Input:
getch()returns key codes. Usecbreak()to get keys immediately andkeypad(stdscr, TRUE)so arrow keys arrive asKEY_UP/DOWN/LEFT/RIGHT. - Streams: After reading to EOF, call
clear()thenseekg(0)(or reopen) before reading again. - CRLF: If the file came from Windows, each line may end with
\r\n.getlinestrips\nbut leaves\r. The code trims trailing\rto prevent cursor resets.
Install build tools and ncurses headers (Debian/Ubuntu/Kali/WSL):
sudo apt update
sudo apt install build-essential libncurses-devCompile:
g++ main.cpp -o textedit -lncursesNote: The
-lncurseslink flag is required at link time.
./texteditUse the /mnt/<drive>/... mapping, e.g. C:\Users\you\file.txt → /mnt/c/Users/you/file.txt.
That’s typically CRLF input. Convert once with:
dos2unix /path/to/file.txt(Or rely on the built-in \r stripping.)
-
Start the program → Menu:
- 1. Open file — enter a path (on WSL, use
/mnt/...). - 2. Create file — placeholder for future work.
- 3. Quit
- 1. Open file — enter a path (on WSL, use
-
In the viewer:
- Arrow keys — move the cursor (rows increase downward).
:thenq— quit.:theni— enter insert/navigation routine (WIP).
.
├─ main.cpp # menu (Setup::setup/state/edit) + Commands::i()
└─ README.md
move(y, x)is (row, col); you must callrefresh()to see it.cbreak()+keypad(stdscr, TRUE)are needed sogetch()returns arrow keys asKEY_*.- When reusing a
std::fstreamafter EOF:file.clear(); file.seekg(0);. - Keep cursor bounds within the window size (
LINES,COLS) and within the current line length for horizontal moves.
- Real insert mode (edit
linesin memory; write back to disk). - Scrolling viewport (
firstVisibleLine, re-render region). - Status bar (filename, row:col, mode).
- Search (
/pattern), jump to line.
- Undefined references to
initscr/printw— link with-lncurses. - Arrow keys print weird characters — ensure
cbreak()andkeypad(stdscr, TRUE)are set. - Can’t open
C:\...from WSL — use/mnt/c/...mapping. - Screen doesn’t update — call
refresh()after drawing or moving the cursor.
MIT (or your choice).