Part 1 · Basics · Lesson 07 · 45 minutes
Edit files in the terminal
- Open, change, save and close a file with nano
- Use the modes of vim to change, search, undo and save a file
- Name the main keys of Midnight Commander, an optional file manager
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A server that you look after has no desktop. You reach it over SSH, and you see only a terminal. A review finds that its settings file allows a login as root with a password, and your team lead says: "Change it, today. You have no mouse and no editor window." In your lab, the file is a copy of the real one. The task is to edit it with the tools that every server has.
Recall
What does "echo hi | tee -a out.txt" do that "echo hi > out.txt" does not?
It prints hi on the screen as well, and with -a it adds the line to the end of out.txt instead of replacing the file.
What does "grep -q word file && echo yes" print when the word is not in the file?
Nothing. grep -q ends with a failure status when no line matches, so && does not run echo.
The idea
Sooner or later you change a file on a machine that has only a text terminal: a settings file, a script, a rule. A terminal editor is a program that opens a file, lets you change it with the keyboard, and writes it back. Three are worth knowing.
| Editor | When to use it |
|---|---|
nano | Quick to learn. The keys are listed at the bottom of the screen. |
vim | On almost every server, in the form of vi. Fast once you know it, strange at first. |
Midnight Commander (mc) | A file manager with two panels and a built-in editor. It is not always installed. |
Nano is a modeless editor: whatever you type goes into the file. Vim has modes: the same key does a different thing in each mode. This is the cause of most trouble of new users, so learn the picture first.
- Normal mode → Insert mode: press i, a or o
- Insert mode → Normal mode: press Esc
- Normal mode → Command line: press :
- Command line → Normal mode: press Enter or Esc
Vim starts in Normal mode. There, a key is a command (dd deletes a line). In Insert mode, a key is text. In the command line, you type a command such as :wq and press Enter. When you are lost, press Esc and you are back in Normal mode.
Worked example
1. Make a file to edit
Make the lab folder and a file with the settings of a pretend server, in the form of a real SSH server file. The # line is a comment.
mkdir -p ~/krlab/linux-basics/editors && cd ~/krlab/linux-basics/editors
cat > server.conf <<'EOF'
# lab server settings
Port 22
PermitRootLogin yes
PasswordAuthentication yes
MaxAuthTries 6
EOF2. Change a setting with nano
nano FILE opens the file. In its bottom bar, ^ means the Ctrl key, so ^O is Ctrl+O. Run it.
nano server.conf GNU nano 8.7.1 server.conf
# lab server settings
Port 22
PermitRootLogin yes
PasswordAuthentication yes
MaxAuthTries 6
[ Read 5 lines ]
^G Help ^O Write Out ^F Where Is ^K Cut ^T Execute ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To LineNow press, in this order:
Ctrl+W, typePermitRoot, press Enter. This is the search. The cursor jumps to the word. (The screen above is nano 8.7.1, whose bar shows^Ffor search. Ubuntu 24.04 has nano 7.2, whose bar shows^W Where Is.Ctrl+Wworks in both. Read your own bar.)Ctrl+Emoves the cursor to the end of the line. Press Backspace three times to deleteyes, and typeno.Ctrl+O(write out) asks for the file name. Press Enter to keep it. The bar says[ Wrote 5 lines ].Ctrl+Xleaves nano. If you changed the file and did not save, nano asks "Save modified buffer?".Ysaves,Nthrows the changes away,Ctrl+Ccancels the exit.
cat server.conf# lab server settings
Port 22
PermitRootLogin no
PasswordAuthentication yes
MaxAuthTries 6Other nano keys: the arrow keys move, Ctrl+K cuts the whole line, Ctrl+U pastes it back, and Alt+U undoes the last change.
3. Change the file with vim
vim FILE opens the file in Normal mode. Press these keys. Each one runs at once: do not press Enter after a single key.
| Keys | What happens |
|---|---|
/Max Enter | Search forward. The cursor goes to MaxAuthTries. n finds the next match. |
dd | Delete the line that the cursor is on. |
u | Undo. The line is back. Ctrl+R redoes. |
o | Open a new empty line below, and enter Insert mode. The bottom shows -- INSERT --. |
LogLevel DEBUG | Text. In Insert mode, it goes into the file. |
Esc | Back to Normal mode. |
:wq Enter | Write the file and quit. |
vim server.conf
cat server.conf# lab server settings
Port 22
PermitRootLogin no
PasswordAuthentication yes
MaxAuthTries 6
LogLevel DEBUGYou can also leave without saving: :q quits, and fails with E37: No write since last change (add ! to override) if you changed something. :q! quits and throws the changes away. :w writes and stays.
More Normal mode keys: h j k l or the arrows move, 0 and $ go to the start and the end of the line, gg and G to the first and the last line, x deletes one character, yy copies a line, p pastes it, i starts Insert before the cursor, and A starts Insert at the end of the line.
While a file is open, vim keeps a swap file next to it (.server.conf.swp). It is removed when you leave. At exit, vim also writes ~/.viminfo, which holds your search words.
4. Browse with Midnight Commander
This part was not tested on the build machine, because mc is not installed there. On Debian and Ubuntu you install it with sudo apt install mc.
mc shows two panels, each a folder. The left and the right panel can show different folders, and one of them is active. The bottom line lists the function keys.
| Key | What it does in the panels |
|---|---|
Tab | Make the other panel active. |
| Arrows, Enter | Move; open a folder, or run a file. Enter on .. goes up. |
Insert | Mark a file, to act on several at once. |
F3, F4 | View a file, or edit it with the built-in editor mcedit. |
F5, F6 | Copy, or move, the marked files to the folder of the other panel. |
F7, F8 | Make a folder, or delete the marked files. |
F9, F10 | Open the top menu, or quit. |
In the editor mcedit, F2 saves, F7 searches, F3 starts and ends a marked block of text, F5, F6 and F8 copy, move and delete that block, and F10 leaves. If your terminal takes the function keys for itself, press Esc and then the digit: Esc 1 for F1, and Esc 0 for F10. mcedit FILE opens the editor alone.
Your turn
Run this block once. It builds the file to edit.
mkdir -p ~/krlab/linux-basics/editors && cd ~/krlab/linux-basics/editors
cat > app.conf <<'EOF'
# app settings
mode = test
debug = on
user = guest
retries = 5
EOFThere are no steps here. Edit app.conf as follows, and save after each edit.
- With
nano: change the linedebug = onintodebug = off. - With
vim: delete the lineuser = guest, and add the new last linetimeout = 30.
Then check the file with the tools of lesson 4.
Check yourself
You typed text in vim, and it did not appear as text. Instead the screen jumped. What happened, and how do you get out?
You were in Normal mode, where keys are commands. Press Esc to be sure that you are in Normal mode, and i to insert text. To leave without saving, type :q! and press Enter.
In nano, what is the difference between Ctrl+O and Ctrl+X?
Ctrl+O writes the file and stays in nano. Ctrl+X leaves nano, and asks about saving when the file changed.
Why is a terminal editor useful when a desktop editor is better to look at?
On a server over SSH there is often no screen to draw windows. A terminal editor works over any text connection, uses almost no memory, and is installed already. A skilled administrator uses it for a quick change on a machine that is not theirs to set up.
You pressed F10 in mc and nothing happened. What can you try?
Some terminals take the function keys for their own menu. Press Esc, then 0, which means F10.
Field notes
- Edit a file that root owns with
sudoedit FILE(orsudo -e), not withsudo nano. It edits a copy as your own user, so the editor runs without root rights. Edit the sudo rules only withvisudo, which checks the syntax before it saves. - Editors leave traces. Vim keeps
~/.viminfowith search words and copied text, and swap files next to the file. Do not edit a secret on a shared machine without a look at these files. - Read a file without a risk of changing it:
vim -R FILEopens it read-only, andless FILEonly shows it. - A wrong edit of a settings file can lock you out. Keep a copy (
cp -a sshd_config sshd_config.bak) and test the syntax of the service before you restart it. - The setting in this lab is a real one: the CIS Benchmarks for Linux ask to forbid the direct login of root over SSH, with the line
PermitRootLogin noinsshd_config. Part 3 covers SSH settings.