Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 1 · Basics · Lesson 07 · 45 minutes

Edit files in the terminal

After this lesson you can
  • 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
Lab files of this lesson (8)

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.

EditorWhen to use it
nanoQuick to learn. The keys are listed at the bottom of the screen.
vimOn 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.

The modes of vim
Insert modeNormal modeCommand line1234Insert modeNormal modeCommand line1234
  1. Normal mode → Insert mode: press i, a or o
  2. Insert mode → Normal mode: press Esc
  3. Normal mode → Command line: press :
  4. 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
EOF

2. 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 Line

Now press, in this order:

  1. Ctrl+W, type PermitRoot, press Enter. This is the search. The cursor jumps to the word. (The screen above is nano 8.7.1, whose bar shows ^F for search. Ubuntu 24.04 has nano 7.2, whose bar shows ^W Where Is. Ctrl+W works in both. Read your own bar.)
  2. Ctrl+E moves the cursor to the end of the line. Press Backspace three times to delete yes, and type no.
  3. Ctrl+O (write out) asks for the file name. Press Enter to keep it. The bar says [ Wrote 5 lines ].
  4. Ctrl+X leaves nano. If you changed the file and did not save, nano asks "Save modified buffer?". Y saves, N throws the changes away, Ctrl+C cancels the exit.
cat server.conf
# lab server settings
Port 22
PermitRootLogin no
PasswordAuthentication yes
MaxAuthTries 6

Other 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.

KeysWhat happens
/Max EnterSearch forward. The cursor goes to MaxAuthTries. n finds the next match.
ddDelete the line that the cursor is on.
uUndo. The line is back. Ctrl+R redoes.
oOpen a new empty line below, and enter Insert mode. The bottom shows -- INSERT --.
LogLevel DEBUGText. In Insert mode, it goes into the file.
EscBack to Normal mode.
:wq EnterWrite the file and quit.
The vim command :wq
:opens the command linewwrite the fileqquit vimEnterrun the command:opens the command linewwrite the fileqquit vimEnterrun the command
vim server.conf
cat server.conf
# lab server settings
Port 22
PermitRootLogin no
PasswordAuthentication yes
MaxAuthTries 6
LogLevel DEBUG

You 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.

KeyWhat it does in the panels
TabMake the other panel active.
Arrows, EnterMove; open a folder, or run a file. Enter on .. goes up.
InsertMark a file, to act on several at once.
F3, F4View a file, or edit it with the built-in editor mcedit.
F5, F6Copy, or move, the marked files to the folder of the other panel.
F7, F8Make a folder, or delete the marked files.
F9, F10Open 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
EOF

There are no steps here. Edit app.conf as follows, and save after each edit.

  1. With nano: change the line debug = on into debug = off.
  2. With vim: delete the line user = guest, and add the new last line timeout = 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 (or sudo -e), not with sudo nano. It edits a copy as your own user, so the editor runs without root rights. Edit the sudo rules only with visudo, which checks the syntax before it saves.
  • Editors leave traces. Vim keeps ~/.viminfo with 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 FILE opens it read-only, and less FILE only 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 no in sshd_config. Part 3 covers SSH settings.