Viewing and Editing Files
You can do a surprising amount of work in the command line just by looking at files. You do not need to become a terminal text editor expert to be productive.
This page focuses on reading files safely and understanding your options for editing them.
Viewing files from the command line
cat: Dump the whole file
cat (concatenate) prints a file’s contents straight to the terminal.
cat notes.txt
Use it when:
- The file is short
- You just want a quick look
Downside: For long files, cat will flood your screen and you’ll have to scroll back manually.
less: Scrollable viewer (recommended)
less is a pager—a program that lets you scroll through output one screen at a time.
less notes.txt
Once inside less:
- Press
Spaceto go down a page - Press
bto go back a page - Press
↑/↓to move line by line - Press
/then type text to search - Press
qto quit and return to the shell
Mental model: less is a temporary viewer. It never edits the file; it only shows you what’s inside.
head and tail: Just the beginning or end
Sometimes you only care about the start or end of a file.
# First 10 lines (default)
head server.log
# First 20 lines
head -n 20 server.log
# Last 10 lines (default)
tail server.log
# Last 50 lines
tail -n 50 server.log
Use cases:
headto check file structure or headerstailto see the most recent log entries or output
tail -f: Follow a growing file
tail -f is very useful for watching logs as they update.
tail -f server.log
This:
- Shows the last lines of
server.log - Keeps running
- Prints new lines as they are appended
Press Ctrl+C to stop following and return to the prompt.
Choosing how to edit files
You have three main options:
- Graphical editor (VS Code, PyCharm, etc.)
- Beginner-friendly terminal editor (
nano) - Power-user terminal editors (
vim,emacs) — optional, not required
Using a graphical editor (common in real projects)
Most developers use a graphical editor even when working heavily in the command line.
Examples:
- VS Code:
code - JetBrains tools:
idea,pycharm, etc. (if installed)
# Open a single file
code notes.txt
# Open the current directory as a project
code .
Typical workflow:
- Use the command line for navigation and running commands
- Use your editor for actual writing and editing
This is a perfectly valid (and recommended) way to work as a beginner.
nano: A gentle terminal editor
If you want to stay in the terminal and make small edits, nano is a good starting point.
nano notes.txt
At the bottom, you’ll see commands like:
^O= Ctrl+O (write out / save)^X= Ctrl+X (exit)^K= Ctrl+K (cut line)^U= Ctrl+U (paste line)
Basic nano workflow:
- Open a file:
nano notes.txt - Type or edit text
- Save with Ctrl+O, press Enter to confirm
- Exit with Ctrl+X
vim and emacs: Powerful but optional
You’ll see references to vim and emacs everywhere. They are extremely powerful editors—but they come with a steep learning curve.
For this guide:
- You do not need
vimoremacsto be productive - It’s fine to treat them as advanced tools for later
If you accidentally open vim and get stuck:
- Press
Esc - Type
:q! - Press Enter
That quits without saving.
Combining viewing commands with other tools
Viewing commands become more powerful when you combine them with other commands using pipes (covered in detail in the redirection and pipes section).
Examples:
# View only the first 50 lines of a long command’s output
some-long-command | head -n 50
# View search results one page at a time
grep "ERROR" server.log | less
# Follow only lines that match a pattern (using grep -i for case-insensitive)
tail -f server.log | grep -i "error"
You don’t need to memorize these patterns now. The important idea is:
You can send output from one command into viewers like
less,head, andtailto keep things manageable.
Summary
- Reading files:
catprints the whole file (best for short files)lesslets you scroll, search, and then quit cleanlyhead/tailshow the beginning or end of filestail -fis great for watching logs update in real time
- Editing files:
- It’s normal to use VS Code or another GUI editor, even as a heavy CLI user
nanois a beginner-friendly terminal editor if you want to stay in the shellvim/emacsare powerful but optional for beginners
- You can combine viewing commands with other tools using pipes to keep output readable and focused.