$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

Editing Files: nano and vim Survival Basics

5 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:20

Why an editor needs modes or shortcuts

A terminal sends every key press to the program that is running in front of it. An editor must therefore decide what each key means. Most editors use modifier keys, such as Control, to separate commands from text. nano does that, and shows the most common commands along the bottom of the screen. vim goes further and uses modes: in normal mode, letters are commands, and in insert mode, letters are text. The mode is shown in the status line.

Both editors write bytes to a file in the same way the shell does. They keep the file descriptor open, write the new contents, and close it. Some editors write a new file and rename it, which the hard link unit explained. Neither editor changes the encoding unless you tell it to, so a file in UTF-8 stays in UTF-8.

nano: the shortcuts on screen

Start nano with a file name. Type text as you would in a normal program. The caret notation in the bottom bar means Control. The most useful keys are Control-O to write the file out, Control-X to exit, Control-W to search, Control-K to cut a line, Control-U to paste it, and Control-G for help. Control-X asks whether to save if you have changed the file, so answer the prompt before you leave.

nano is a good editor for quick changes to configuration files and for notes. It does not offer the speed of vim for large edits, but it has no mode to learn, and the on-screen help covers the rest.

vim: the three modes you need

vim starts in normal mode. Normal mode moves the cursor with h, j, k, and l, and it runs commands. The key i enters insert mode, where letters are inserted as text, and the Escape key returns you to normal mode. The key a appends after the cursor, and o opens a new line below. Command-line mode begins with a colon, and it is where you save and quit: :w writes, :q quits, :wq does both, and :q! quits without saving.

Knowing these keys is enough to edit any file. A few more are worth learning early. The key dd deletes a line, yy copies it, p pastes below the cursor, and u undoes the last change. Searching uses a slash followed by a pattern, and the key n repeats the search.

BASH4 lines
cd /workspace
printf 'hello\n' > note.txt
vim -es -c 'normal A world' -c wq note.txt
cat note.txt

The command runs vim in silent Ex mode, which is how a script edits a file without a screen. It moves to the end of the first line, appends the word, and writes and quits. The output is hello world.

Note

The -e and -s options put vim into Ex mode, which reads commands and does not need a terminal. Each -c option runs one command, in order. This is a reliable way to make repeatable edits in a script, and it avoids interactive prompts.

Scripted substitution

The same Ex mode can run substitutions. The command %s/old/new/ replaces the first match on every line, and adding a trailing g replaces every match on each line. Scripted edits are useful when you need the same change in many files, and they are easier to review than a sequence of manual edits. Test on a copy first, because a bad pattern affects every match.

For plain text replacement in a pipeline, sed is often simpler. Vim in Ex mode is most useful when you want the editor's own commands, such as moving between windows or using its regular expressions with its settings.

nano version and checking the install

Both editors can print their version. The option --version for nano prints a line that starts with GNU nano and the release number. It is a quick check that the editor is installed before a script tries to run it, and it is the same habit as checking the help of any program when no manual is available.

Warning

Do not run vim with sudo on a file you are not sure about. Editing a system file as root can break a machine, and a stray save may replace a configuration that the system needs to boot. Make a copy first and edit the copy.

Choosing an editor for the job

nano is the right choice for a one-off change to a configuration file when you want the shortcuts visible and no modes to remember. vim is the right choice for repeated edits, for editing very large files, and for scripted changes. Use vim -R to open a file read-only when you want to look without the risk of saving a change by accident, and vim -d to show the differences between two files side by side.

Both editors create temporary swap or backup files while you edit. If the session ends badly, a swap file can remain and vim will warn you the next time you open the file. Read the warning, decide whether the swap holds unsaved work, and only then delete it. Ignoring the warning is the usual way to lose an edit.

Whatever editor you choose, save early and often, and check the result with cat or less before you close the terminal. The editor shows what you typed, but the file on disk is the thing that matters.

Practice in the lab

The lab uses vim in Ex mode to make two edits to a file, and it asks you to save the version of nano. The checks read the final file contents and the version line, so you can see that the scripted edits did what they say.

Terminal lab · 3 steps · real Linux in your browser

Edit a file without a screen

Run vim in silent Ex mode to append and substitute text, and save the nano version.

01
Append a word with vim
Run vim in silent Ex mode. It moves to the end of the first line, appends a word, then writes and quits: ``bash vim -es -c 'normal A world' -c wq note.txt ``
Show hint for step 1
Each -c option runs one command. The normal command runs keys as if you had typed them in normal mode.
02
Check the nano version
Save the first line of nano's version output: ``bash nano --version | head -n 1 > nano-version.txt ``
Show hint for step 2
The first line names the editor and its release.
03
Substitute a word with vim
Replace the first hello with HELLO, then write and quit: ``bash vim -es -c '%s/hello/HELLO/' -c wq note.txt ``
Show hint for step 3
%s applies a substitution to every line, and the trailing slash ends the pattern.
Check yourself Question 1 of 4
Concept Check Entering insert mode recall

In vim's normal mode, which key enters insert mode before the cursor?

Recap & Key Takeaways

nano shows its shortcuts on screen, with Control-O to save and Control-X to exit.
vim has normal and insert modes; i enters insert mode, Escape returns to normal mode, and :wq saves and quits.
vim -es with -c options runs editor commands from a script without a screen.
Substitution with %s is a fast way to make a repeatable change, and it should be tested on a copy first.

Primary Sources & Further Reading

Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue