Text Editors: nano & vim
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.
nano notes/todo.txt
nano -l notes/todo.txt
nano +3 notes/todo.txtThe 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:
| Keys | Action |
|---|---|
Ctrl+O, then Enter | Save ("Write Out"); Enter confirms the file name |
Ctrl+S | Save straight away without the name prompt |
Ctrl+X | Exit; asks "Save modified buffer?" if there are unsaved changes (press Y or N) |
Ctrl+W | Search ("Where Is") |
Ctrl+\ | Search and replace |
Ctrl+K / Ctrl+U | Cut the current line / paste it |
Alt+U / Alt+E | Undo / redo |
Ctrl+_ | Go to a line number |
Ctrl+G | Full 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:
sudo nano /etc/hosts127.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.
| Mode | How to enter it | What keys do |
|---|---|---|
| Normal | Press Esc (vim starts here) | Keys are commands: move, delete, copy, paste |
| Insert | Press i in Normal mode | Keys type text, like any editor |
| Command-line | Press : in Normal mode | Type commands such as save and quit, then Enter |
| Visual | Press v in Normal mode | Select 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
vim notes/todo.txtThen, inside vim:
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 changesIf 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:
| Keys | Action |
|---|---|
h j k l | Left, down, up, right (arrow keys also work) |
gg / G | First line / last line |
0 / $ | Start / end of the line |
dd | Delete (cut) the current line |
yy | Copy ("yank") the current line |
p | Paste below the cursor |
u / Ctrl+R | Undo / redo |
/word then n | Search, then jump to the next match |
o | Open 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:
:%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):
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrcCommon mistakes
- Typing in vim's Normal mode. Letters run as commands and change your file. Press
Esc, thenuto undo, thenibefore 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 withsudofrom 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+Osaves,Ctrl+Xexits,Ctrl+Wsearches. - Use
sudo nanofor files in/etc, and back them up first. - vim has modes:
ienters Insert mode,Escreturns to Normal mode,:starts commands. - In vim,
:wqsaves and quits and:q!quits without saving. vimtutorteaches vim interactively, andEDITORchooses 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
nano practice.txt
vim practice.txt
cat practice.txtMeera 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?
In nano's help lines, ^ stands for the Ctrl key.
2. You opened a file in vim and pressed some keys by mistake. How do you leave without saving anything?
:q! quits and discards changes; Esc first makes sure you are in Normal mode.
3. Which vim key enters Insert mode so you can type text?
i inserts before the cursor; Esc returns to Normal mode.
4. Why does saving /etc/hosts fail when opened with plain nano /etc/hosts?
System files are owned by root; open them with sudo nano.
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
