View and edit configuration files in the terminal

Choose a viewing tool based on file size, and use Vim to open, edit, save, and close files.

🎯 Introduction

On a server, you often view configuration, logs, and code directly in the terminal. This article uses Vim to edit a text file, then explores ways to read short and long files.


🛠 Edit and save a file with Vim

First check for both tools with command -v vim and command -v less. If either is missing, run sudo apt update followed by sudo apt install vim less. sudo lets an authorized account run that command with administrator privileges. User permissions and APT are explained later.

Software installation prompts and Vim's interface may vary by version, language, and configuration. The save instructions use Vim's default key bindings.
vim "$HOME/linux-lab/notes/welcome.txt"
  1. Vim opens in normal mode, where keys move the cursor or issue editing commands. Press lowercase i to enter insert mode, then type Linux lab ready.
  2. Press Esc to return to normal mode. Type :wq, including the leading colon, then press ↩ Return on a Mac keyboard. w writes the file, and q quits.
  3. Back in the terminal, check the saved content with cat "$HOME/linux-lab/notes/welcome.txt".

Use :w to save and continue editing. Use :q! when you want to discard unsaved changes. Enter either command from normal mode, then press ↩ Return. If the file did not exist when you opened it, it is created on disk only after a successful save.


🧱 Choose a viewer based on file size

CommandWhen to use it
cat filePrint a short file all at once, such as configuration or a small script.
less fileRead a long file a page at a time without flooding the terminal.
head -n 5 fileRead only the first 5 lines, often to check a file's format.
tail -n 5 fileRead only the last 5 lines, often to check recent log entries.

Create a practice log with vim "$HOME/linux-lab/logs/app.log", enter the following text, and save. The service management articles will generate logs from a running program later.

INFO server started port=3000
INFO request GET /health status=200
WARN request took 1500ms
ERROR failed to read notes.txt
INFO request GET /health status=200
ERROR startup port already in use
head -n 2 "$HOME/linux-lab/logs/app.log"
tail -n 2 "$HOME/linux-lab/logs/app.log"
less "$HOME/linux-lab/logs/app.log"

In less, use the arrow keys to scroll. Press /, type ERROR, and press ↩ Return to search. Press n for the next match or q to return to the terminal. tail -f keeps waiting for new lines; you will use it later to watch live logs.


❓ Knowledge check

Q1: Why does pressing letters in Vim not insert text?

Vim starts in normal mode. Press i to enter insert mode, then press Esc when finished before running a save command.

Q2: Why does q sometimes quit and sometimes do nothing?

In less, press q directly. In Vim's normal mode, type :q and confirm. Vim prompts you to handle any unsaved changes.


🧪 Exercise

Create notes/commands.txt with Vim and describe pwd and ls on separate lines. Save, then read the lines with head -n 1 and tail -n 1. Change one line, quit with :q!, and use cat to confirm that the previously saved content remains.


🎉 You can now complete a full file editing workflow in the terminal!