Text Editors
At some point you will need to edit a file directly in the terminal. Knowing how to use a terminal text editor is not optional if you want to work with Linux seriously. This post covers nano and vim.
Series: Learning Linux from Scratch
- 1. Learning Linux from Scratch — After a Full IT Apprenticeship
- 2. What is Linux?
- 3. The Filesystem
- 4. Users and Permissions
- 5. Installing and Managing Software
- 6. Text Editors
- 7. Shell Scripting Basics
- 8. Process Management
- 9. Networking Fundamentals
- 10. SSH
- 11. systemd and Services
- 12. Disk Management
- 13. Users and Groups — In Depth
- 14. Cron and Scheduled Tasks
- 15. Firewall — iptables and ufw
- 16. Environment Variables and the Shell
- 17. Log Management
- 18. Kernel Module Management
- 19. The /proc Filesystem — In Depth
- 20. The /sys Filesystem and udev
- 21. Kernel Parameters and sysctl
- 22. Compiling and Installing a Custom Kernel
At some point you will need to edit a file directly in the terminal. A configuration file, a script, a cron job. Knowing how to use a terminal text editor is not optional if you want to work with Linux seriously.
There are several options. This post covers two — nano and vim. Start with nano. Learn vim later when you are ready.
nano
nano is the friendliest terminal text editor. It works like you would expect a text editor to work. You open a file, you type, you save, you exit.
nano filename.txtIf the file does not exist, nano creates it. If it does, nano opens it.
Once inside nano, you just type. No modes, no special commands to enter before you can start writing.
At the bottom of the screen you will always see a list of available commands. The ^ symbol means Ctrl.
The ones you need to know:
Ctrl + O— save the file (Write Out)Ctrl + X— exit nanoCtrl + K— cut the current lineCtrl + U— paste the cut lineCtrl + W— search for text
To save and exit: press Ctrl + O, confirm the filename by pressing Enter, then press Ctrl + X.
A quick example
nano notes.txtType something. Press Ctrl + O, then Enter to confirm. Press Ctrl + X to exit. The file is saved.
cat notes.txtcat prints the contents of a file to the terminal. You will see whatever you typed.
vim
vim has a reputation for being impossible to use. That reputation exists because vim works completely differently from any text editor you have used before. But once it clicks, it is extremely powerful.
The key thing to understand about vim is that it has modes.
When you open vim, you are in Normal mode. In Normal mode, every key on your keyboard is a command. You cannot just start typing.
To start typing, you need to enter Insert mode by pressing i. Now you can type normally. Press Esc to go back to Normal mode.
Opening and exiting vim
vim filename.txtTo exit vim — the question everyone asks at least once:
- Press
Escto make sure you are in Normal mode - Type
:qand press Enter to quit (only works if you have not made changes) - Type
:wqand press Enter to save and quit - Type
:q!and press Enter to quit without saving
Basic vim workflow
vim myfile.txt # open the file
i # enter insert mode
# type your changes
Esc # return to normal mode
:wq # save and exit
Why bother learning vim?
vim is available on almost every Linux system by default, including servers that have no graphical interface and minimal software installed. If you ever SSH into a remote machine and need to edit a file, vim will probably be there. nano might not be.
You do not need to master vim. Knowing how to open a file, make a change, and exit is enough to start with.
For everyday editing on your own machine, use nano. It is simple and it works. Start learning vim slowly on the side — a few commands at a time. It is worth the investment.
Next up: shell scripting — how to write scripts that automate tasks you would otherwise type manually.