Text Editors: nano & vim

Linux · Lesson 8 of 32 7 min read Updated

Sooner or later you must change a file on a machine that has no mouse and no desktop, such as a cloud server reached over SSH. For that you need a terminal text editor, a program that edits files inside the terminal window.

Two editors are installed almost everywhere. nano is simple and shows its shortcuts on screen, so it is the best one to start with. vim is harder at first but extremely fast once learned, and it is the one you can rely on finding on every Unix-like system. This lesson teaches nano properly and gives you the vim basics you need to survive.

nano: the friendly editor

Open a file by passing its name. If the file does not exist, nano creates it when you save.

Terminal
nano notes/todo.txt
nano -l notes/todo.txt
nano +3 notes/todo.txt

The first command opens the file. -l shows line numbers down the left side, and +3 opens the file with the cursor on line 3, which is useful when an error message names a line.

You type straight away; there are no modes. The two bottom lines show shortcuts, where ^ means the Ctrl key and M- means the Alt key. So ^X Exit means press Ctrl+X. The ones you need most:

KeysAction
Ctrl+O, then EnterSave ("Write Out"); Enter confirms the file name
Ctrl+SSave straight away without the name prompt
Ctrl+XExit; asks "Save modified buffer?" if there are unsaved changes (press Y or N)
Ctrl+WSearch ("Where Is")
Ctrl+\Search and replace
Ctrl+K / Ctrl+UCut the current line / paste it
Alt+U / Alt+EUndo / redo
Ctrl+_Go to a line number
Ctrl+GFull help

A typical session: open the file, make your change, Ctrl+O, Enter, Ctrl+X. That is all you need for most configuration work.

Editing system files

Files in /etc belong to root, so a normal user can read many of them but not save changes. Open them with sudo:

Terminal
sudo nano /etc/hosts
Output
127.0.0.1 localhost
127.0.1.1 laptop
192.168.1.40 printer.home

Here Priya added the last line so that the name printer.home points to her printer's address. If you forget sudo, nano still opens the file but shows a warning, and saving fails with "Permission denied". Close it and reopen with sudo; do not try to fight it.

vim: modes are the key idea

vim (Vi IMproved) is a newer version of the classic vi editor; on Ubuntu, typing vi usually starts vim. Its big idea is modes: keys mean different things depending on the mode you are in.

ModeHow to enter itWhat keys do
NormalPress Esc (vim starts here)Keys are commands: move, delete, copy, paste
InsertPress i in Normal modeKeys type text, like any editor
Command-linePress : in Normal modeType commands such as save and quit, then Enter
VisualPress v in Normal modeSelect text to copy or delete

Beginners get stuck because they start typing in Normal mode, and their letters run as commands. Remember: i to type, Esc to stop typing.

Opening, saving and quitting vim

Terminal
vim notes/todo.txt

Then, inside vim:

Text
i              start typing (Insert mode)
Esc            back to Normal mode
:w             save (write)
:q             quit
:wq  or  :x    save and quit
:q!            quit and throw away changes

If vim refuses to quit with "E37: No write since last change", it is protecting your unsaved edits. Use :wq to keep them or :q! to discard them.

Moving and editing in Normal mode

Once you can get in and out, these Normal-mode commands make vim fast:

KeysAction
h j k lLeft, down, up, right (arrow keys also work)
gg / GFirst line / last line
0 / $Start / end of the line
ddDelete (cut) the current line
yyCopy ("yank") the current line
pPaste below the cursor
u / Ctrl+RUndo / redo
/word then nSearch, then jump to the next match
oOpen a new line below and enter Insert mode

Commands can take a number: 3dd deletes three lines, 5j moves down five lines. In Command-line mode, :set number shows line numbers and :25 jumps to line 25.

Advanced: search and replace in vim

vim can replace text across a whole file with one command:

Text
:%s/Pune/Pune (MH)/g
:%s/http:/https:/gc

% means every line, s means substitute, then comes /old/new/. The g flag replaces every match on a line, not just the first, and c asks you to confirm each one with y or n. The same s/old/new/g pattern appears again in the sed command later in this course.

Choosing your default editor

Some commands open an editor for you, such as crontab -e, visudo and git commit. They use the editor named in the EDITOR variable. Set it in your shell settings (see Environment Variables, PATH & .bashrc):

Terminal
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc

Common mistakes

  • Typing in vim's Normal mode. Letters run as commands and change your file. Press Esc, then u to undo, then i before typing.
  • Not knowing how to quit vim. Esc, then :q! and Enter always gets you out without saving.
  • Editing system files without sudo. The save fails at the end and you lose your work. Open with sudo from the start.
  • Using a word processor. Programs like LibreOffice Writer add hidden formatting. Config files and scripts must be plain text.

Key points

  • nano is the easiest terminal editor: Ctrl+O saves, Ctrl+X exits, Ctrl+W searches.
  • Use sudo nano for files in /etc, and back them up first.
  • vim has modes: i enters Insert mode, Esc returns to Normal mode, : starts commands.
  • In vim, :wq saves and quits and :q! quits without saving.
  • vimtutor teaches vim interactively, and EDITOR chooses which editor other tools open.

Exercise

Create a file practice.txt with nano containing three lines: your name, your city and your favourite food. Save and exit. Then open the same file in vim, delete the city line, add a new last line "Learning Linux", save and quit. Finally print the file with cat to check.

Show solution
Terminal
nano practice.txt
vim practice.txt
cat practice.txt
Output
Meera Nair
Appam and stew
Learning Linux

In nano: type the three lines, then Ctrl+O, Enter, Ctrl+X. In vim: press j to move to the city line, dd to delete it, G to go to the last line, o to open a new line and start typing "Learning Linux", then Esc and :wq.

Quick Quiz

1. In nano, what does ^X in the shortcut bar mean?

2. You opened a file in vim and pressed some keys by mistake. How do you leave without saving anything?

3. Which vim key enters Insert mode so you can type text?

4. Why does saving /etc/hosts fail when opened with plain nano /etc/hosts?

Learn Linux with a mentor

Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call