Terminal Text Editors: Nano and Vim
Many servers do not have a graphical desktop. When you connect over SSH and need to adjust a configuration file, write a short note, or inspect a cron entry, you need a terminal editor. Nano is easy to learn because text entry works normally. Vim is modal, which is harder at first but efficient once the basic commands are familiar.
The editor is only one part of safe administration. Before editing an important file, make a backup. After editing, verify both the file content and the service-specific syntax before reloading anything. The examples below use only ~/lab/editor.
Prepare a Safe Editing Lab
mkdir -p ~/lab/editor
printf 'server_name=demo\nport=8080\nmode=test\n' > ~/lab/editor/app.conf
cp -a ~/lab/editor/app.conf ~/lab/editor/app.conf.bak
ls -l ~/lab/editor/app.conf*
Expected output:
-rw-r--r-- ... /home/<username>/lab/editor/app.conf
-rw-r--r-- ... /home/<username>/lab/editor/app.conf.bak
The backup exists before any edit. That is the habit to keep.
Warning
Do not practice on real files under /etc. Administrative files may require sudo, but higher privilege does not protect you from typing the wrong value. Use a VM snapshot, a file backup, and a validation command before changing production configuration.
Nano: Direct Editing
Open the lab file:
Nano shows the file in the main area and key hints at the bottom. In Nano notation, ^ means Ctrl, and M- means Meta, usually Alt. ^O means Ctrl+O, not the characters ^ and O.
Common Nano keys:
| Action | Key |
|---|---|
| Help | Ctrl+G |
| Search | Ctrl+W or Ctrl+F on newer Nano |
| Save current file | Ctrl+O, then Enter |
| Exit | Ctrl+X |
| Cut current line | Ctrl+K |
| Paste cut text | Ctrl+U |
| Copy current line | Alt+6 |
| Undo / redo | Alt+U / Alt+E |
| Go to line and column | Alt+G |
Open at a specific line:
This places the cursor at line 2, column 1 on GNU Nano. Check nano --help or man nano on the target system if a shortcut behaves differently; Nano key bindings have changed across versions.
Nano Practice
Inside Nano:
- Change
port=8080toport=9090. - Search for
mode. - Change
mode=testtomode=staging. - Save with
Ctrl+O, confirm withEnter, then exit withCtrl+X.
Verify in the shell:
Expected content:
To roll back:
Vim and Vi: Modal Editing
vi is the traditional Unix editor interface. Vim, "Vi Improved", adds many features while keeping Vi-style commands. On some distributions, vi is a small Vim build; on others it may be a different Vi-compatible editor.
Check what you have:
If vim is not available, use vi for the basic commands in this article.
Open a copy:
The Three Modes Beginners Need
Most Vim confusion comes from modes:
- Normal mode: navigation, deletion, copy, paste, search, and commands. Vim starts here.
- Insert mode: text entry. Enter it with
i,a, oro. - Command-line mode: saving, quitting, settings, and help. Enter it from Normal mode with
:.
If you are unsure where you are, press Esc. That returns to Normal mode in normal situations.
Minimal Vim Commands
Normal mode movement:
| Action | Key |
|---|---|
| Left, down, up, right | h, j, k, l |
| Next / previous word | w / b |
| Start / end of line | 0 / $ |
| Go to file start / end | gg / G |
| Search forward | /text, then Enter |
| Next / previous match | n / N |
Editing:
| Action | Key |
|---|---|
| Insert before cursor | i |
| Append after cursor | a |
| Open new line below | o |
| Delete character | x |
| Delete line | dd |
| Copy line | yy |
| Paste after cursor | p |
| Change word | cw |
| Undo / redo | u / Ctrl+R |
Command-line mode:
:w: write the file.:q: quit if there are no unsaved changes.:wq: write and quit.:q!: quit and discard unsaved changes.:set number: show line numbers.:15: jump to line 15.:help :write: open help for:write.
Note
:w! does not magically grant permission. The process still needs write access. Starting an editor with sudo can also run editor plugins and configuration as root. Follow your team's approved method for privileged edits.
Saving Without Write Permission
If you open a file you cannot write to and try to save, Vim tells you directly rather than saving silently or crashing:
This appears on the command line at the bottom of the screen after :w. It means exactly what it says: the process editing the file does not have write permission on that path. Common causes, in order of likelihood:
- you opened the file as a regular user but it is owned by root or another service account;
- the directory itself, not just the file, denies write (renaming a file during save requires write access to its parent directory);
- the filesystem is mounted read-only.
Check which one applies before reaching for sudo:
ls -l -- <file>
ls -ld -- "$(dirname -- <file>)"
mount | grep -w "$(df --output=target -- <file> | tail -n 1)"
If the file genuinely requires elevated privilege, the safer pattern is to reopen it with sudo, not to fight the running instance:
This writes the buffer through sudo tee back into the same filename (% is the current filename in Vim) without restarting the editor and losing your unsaved changes. Confirm the result afterward with cat or diff against your backup, the same as any other privileged edit.
Vim Practice
Inside vi ~/lab/editor/vim.conf:
- Press
/port, thenEnter, to find the port line. - Press
0to move to the start of the line. - Move to
8080, then presscw9090, thenEsc. - Run
:set number. - Run
:wq.
Verify:
Only the planned value should differ. In later text-processing lessons, tools such as diff make this comparison precise; for now, the key workflow is backup, edit, inspect, and validate.
To practice Vim safely:
If vimtutor is not installed, use the built-in help from inside Vim or Vi with :help.
Read-Only Viewing
If you only want to inspect a small file, use a viewer:
For editor-based read-only intent:
Read-only mode is a guardrail, not a security boundary. It can often be overridden from inside the editor if the process has permission to write. If you truly must avoid changes, use cat, less, or a copied file.
Swap Files and Concurrent Editing
Vim may warn about a swap file when the same file appears to be open elsewhere or a previous edit crashed. Do not delete the swap file automatically.
The warning is a full-screen prompt, not a one-line message, and it looks like this:
E325: ATTENTION
Found a swap file by the name ".app.conf.swp"
owned by: admin dated: Tue Jul 29 10:15:03 2026
file name: ~admin/lab/editor/app.conf
modified: YES
user name: admin host name: web01
process ID: 18422
While opening file "app.conf"
dated: Tue Jul 29 10:12:40 2026
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r app.conf"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".app.conf.swp"
to avoid this message.
Swap file ".app.conf.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (D)elete it, (A)bort:
Read the two numbered paragraphs before choosing a letter — they are Vim telling you exactly which of the two situations you are in. The last line is the actual prompt:
O(Open Read-Only) — safest default when you are not sure. You can inspect the file without risking a second writer.E(Edit anyway) — proceed knowing another process, or you in another terminal, may already hold this file open. Two writers can silently overwrite each other's changes on save.R(Recover) — same as runningvim -r <file>directly; Vim replays the swap file's changes into a new buffer so you can inspect and re-save what was lost when the previous session crashed.D(Delete it) — only correct once you have confirmed, viapsor by asking your team, that no other live session owns the file and that you do not need the recovery data. Deleting first and asking questions later can permanently discard someone else's unsaved work.Q(Quit) /A(Abort) — back out and investigate before touching anything.
Check before answering:
- Is another terminal still editing the file? (
ps -ef | grep -- "vi[m]* .*app.conf"on the same host, or ask whoever else has access.) - Did a previous session crash with unsaved changes? The
process IDline in the prompt tells you which PID owned the session —ps -p <pid>shows whether that process still exists. - Do you need recovery with
vi -r <file>?
If unsure, quit (Q) without editing. Removing an old swap file is safe only after you know no active editor owns it and no recovery data is needed.
Practical Scenario: Change One Configuration Value
Problem: a service port needs to change from 8080 to 9090. The service is fictional, so the validation is simple and local.
Create and back up:
config="$HOME/lab/editor/app.conf"
cp -a -- "$config" "$config.before-port-change"
cat "$config.before-port-change"
Edit only the port line in Nano or Vim.
Verify the result:
Expected verification:
Rollback if needed:
In production, replace the simple grep check with the real validator: nginx -t, sshd -t, systemd-analyze verify, an application config test command, or a dry-run provided by the software.
Common Mistakes
- Typing Nano hints such as
^Oliterally instead of pressingCtrl+O. - Forgetting to save in Nano after changing text.
- Trying to type in Vim while still in Normal mode.
- Using
:qin Vim when there are unsaved changes; use:wqto save or:q!to discard. - Editing production files without a backup and validation command.
- Ignoring Vim swap warnings without checking for another active session.
- Assuming read-only editor mode is the same as filesystem permission protection.
Exercises
- Use Nano to change
mode=testtomode=dev, save, exit, and verify withcat. - Restore the backup, then use Vi or Vim to change only the
portline. - Open the file with
viewand confirm how the editor indicates read-only mode. - Run
vimtutorif available and complete at least the first movement and insert sections.
Verification criterion: you should be able to make one planned edit, prove what changed, and roll it back from a backup.
References
- GNU Nano manual: https://www.nano-editor.org/dist/latest/nano.html
- Ubuntu
nano(1)manual page: https://manpages.ubuntu.com/manpages/noble/man1/nano.1.html - Vim documentation index: https://vimhelp.org/
- Vim first steps: https://vimhelp.org/usr_02.txt.html
- Ubuntu
vim(1)manual page: https://manpages.ubuntu.com/manpages/noble/man1/vim.1.html