Skip to content

Phase 3 · Free

3.6 Editing Files with nano or vim

Linux settings are plain text files. On a server there is no graphical editor, so you edit in the terminal. Pick one editor and learn it well enough to open, change, save, and leave without panic. nano is easier. vim is everywhere and faster once learned.

Phase 3. Lesson 6 of 7. About 25 minutes.

Plain entry

Linux settings are plain text files. On a server there is no graphical editor, so you edit in the terminalThe window or screen where you type text commands and read their output, connected to a shell that runs those commands. Read more →. Pick one editor and learn it well enough to open, change, save, and leave without panic. nano is easier. vim is everywhere and faster once learned. Either is fine for this course.

Terms

  • Config file: a text file a program reads its settings from, usually in /etc.
  • Modal editor: an editor where keys do different things depending on mode. vim is modal; nano is not.
  • BackupA copy of data stored separately from the original, used to restore systems and files after data loss from hardware failure, deletion, ransomware, or disaster. Read more → copy: a copy of a file made before editing, so you can undo.

Depth

Always back up first

sudo cp /etc/hosts /etc/hosts.bak

nano

nano ~/lab/notes.txt

Type normally. The bottom shows shortcuts; ^ means Ctrl.

  • Ctrl+O then Enter: save
  • Ctrl+X: exit
  • Ctrl+W: search
  • Ctrl+K / Ctrl+U: cut / paste a line
  • Alt+U: undo

vim, the minimum

vim ~/lab/notes.txt
  • Press i to start typing (insert mode).
  • Press Esc to stop typing (normal mode).
  • In normal mode: :w save, :q quit, :wq both, :q! quit without saving.
  • /word searches, u undoes, dd deletes a line. Ubuntu ships only a small vim (vim-tiny) with no tutorial. Run sudo apt install vim first; that adds the full vim and vimtutor, a free 30-minute built-in tutorial.

Edit a real system file

sudo nano /etc/hosts

Add a line at the bottom:

127.0.0.1   bnlc-test.local

Save, exit, then prove it worked:

ping -c 1 bnlc-test.local
PING bnlc-test.local (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.040 ms

Then remove the line, or restore: sudo cp /etc/hosts.bak /etc/hosts.

Going further

diff /etc/hosts.bak /etc/hosts shows exactly what you changed. sudoedit /etc/hosts edits a copy as you and writes it back as root, which is safer than running a whole editor as root.

Why it works this way

You back up before editing because a config file is code the system runs. One typo in /etc/fstab or /etc/sudoers can stop a boot or lock you out, and the backup turns a crisis into one cp. You prove the edit with ping because a saved file is not the same as a working change. Always test the thing the change was supposed to do.

Self-check

  1. How do you save and quit in nano? In vim?
  2. What should you do before editing anything in /etc?
  3. How do you see what you changed?

Answers: (1) nano: Ctrl+O, Enter, Ctrl+X. vim: Esc, :wq. (2) Make a backup copy. (3) diff the backup and the file.

Sources