Skip to content
$ lynuxpro
← Linux

LESSON 5 OF 26 · BEGINNER · 15 MIN

Editing files with nano and vim

In one minute

  • nano and vim are text editors that run inside the terminal.
  • A server has no VS Code and no mouse. When a config needs fixing at 2 a.m., you edit it right there in the terminal.
  • nano works like a normal notepad: you just type, and the shortcuts are shown at the bottom. vim has modes: one mode for typing text and another where keys are commands.
  • 👉 Any quick change to a file on a server: a config value, a hosts entry, a typo in a script. For changes on many servers, you use automation instead (see "Editing without an editor").

You do not need to master vim. You must be able to open a file, change it, save it and quit without panic.

nano: the easy one

A simple editor. What you type goes straight into the file.

👉 Your first choice for quick edits, if it is installed.

nano /etc/hosts
OUTPUTterminalutf-8 · 1L

nano shows its shortcuts at the bottom of the screen. ^ means the Ctrl key.

KeysWhat it does
Type normallyAdd text
Ctrl+O, then EnterSave ("write Out")
Ctrl+XExit
Ctrl+WSearch ("Where is")
Ctrl+K / Ctrl+UCut a line / paste it back
Alt+UUndo

Remember it: O = save Out, X = eXit.

vim: the one that is always there

vi is on almost every Linux and Unix system, even tiny containers and network devices. vim ("vi improved") is the modern version.

On a minimal server, nano may be missing, but vi is almost always there. How it works: modes. This is the one idea that makes vim make sense. Keys do different things depending on the mode you are in.

ModeWhat keys doHow to get there
NormalKeys are commands (move, delete, copy)Press Esc
InsertKeys type textPress i
Command-lineRun commands like save and quitType : from normal mode

Think of it like: a TV remote with a "typing mode" button. In normal mode the buttons change channels. Press i and they type letters. Press Esc to get the remote back.

The survival sequence

Learn just these five steps first:

vim notes.txt     open the filei                 start typing (insert mode)...type...Esc               back to normal mode:wq               write and quit
OUTPUTterminalutf-8 · 5L
CommandMeaning
:wSave (write)
:qQuit (refuses if you have unsaved changes)
:wq or :xSave and quit
:q!Quit and throw away your changes. Your emergency exit

Stuck in vim? Press Esc twice, type :q!, then press Enter. You are out, and nothing was changed.

Moving and editing faster (normal mode)

👉 Once the survival sequence feels easy, add these one at a time.

KeysWhat it does
gg / GGo to the top / bottom of the file
0 / $Start / end of the line
/text then nSearch for "text", then jump to the next match
ddDelete (cut) the current line
yy / pCopy (yank) the line / paste it below
u / Ctrl+RUndo / redo
:set numberShow line numbers
:%s/old/new/gReplace every "old" with "new" in the whole file
:42Jump to line 42

A real example: an error says the problem is on line 37 of a config. Open the file, type :37, fix it, then :wq.

Editing without an editor

Changing a file with a single command, no editor opened.

Scripts and automation cannot press keys in an editor.

👉 In scripts, CI/CD jobs, and any time the same change must happen on many servers.

echo "10.0.3.10 cache1" >> /etc/hostssed 's/web1/web-1/g' /etc/hosts
SHELLbashutf-8 · 2L
  • The first line adds a line to the end of the file.
  • sed 's/old/new/g' file prints the file with every "old" replaced by "new". sed -i changes the file itself. You will learn sed properly in the Bash Scripting course.

In the practice terminal, nano and vim explain how to write files instead, because full-screen editors need a real machine. Open a live lab to practise them for real.

nano notes.txt
SHELLbashutf-8 · 1L

Real-world scenario: changing an nginx setting safely

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak   # 1. back upsudo vim /etc/nginx/nginx.conf                            # 2. editsudo nginx -t                                             # 3. test the configsudo systemctl reload nginx                               # 4. apply
OUTPUTterminalutf-8 · 4L

Back up → edit → test → apply. Many services have a "test my config" command (nginx -t, sshd -t, apachectl configtest). Use it every time. A broken config plus a restart means the service goes down.

Common mistakes

  • Typing text in vim's normal mode. Your letters become commands and change things. Press u to undo, Esc, then i before you type.
  • Editing a root-owned file as a normal user, then not being able to save. Quit with :q! and open it again with sudo.
  • Ignoring swap-file warnings. If vim says a .swp file exists, someone else may be editing the same file, or an earlier session crashed. Read the message before you choose.
  • Windows line endings. Files edited on Windows can carry hidden \r characters. These break shell scripts with "bad interpreter" errors.

Practice

  1. In a live lab, open notes.txt in nano, add a line, save and exit.
  2. Open the same file in vim, delete the first line with dd, undo with u, then quit without saving using :q!.
  3. In vim, replace every "devops" with "DevOps" using :%s/devops/DevOps/g and save.
  4. Without any editor, add a line to a file with >> and show it with tail -n 1.

Cheat sheet

Tasknanovim
Opennano filevim file
Start typingJust typei
SaveCtrl+O, Enter:w
QuitCtrl+X:q
Quit without savingCtrl+X, then N:q!
SearchCtrl+W/text
UndoAlt+Uu

Key takeaways

  • nano is the easy editor: just type, then Ctrl+O to save and Ctrl+X to exit.
  • vim is on every server. Press i to type, Esc to stop, :wq to save and quit.
  • :q! gets you out of vim without saving.
  • Back up a config, edit it, test it, and only then reload the service.