Text editors: nano & vim basics
Edit config files with nano, survive and thrive in vim, and set your default editor.
Sooner or later every Linux user has to edit a file in the terminal: a config file on a server, a crontab, a commit message, a script. There's no mouse-driven GUI on a server, so you need a terminal text editor. You'll learn two: nano, which is simple and beginner-friendly, and vim, which is powerful, everywhere, and well worth knowing the basics of.
nano: the friendly editor#
nano works like a normal editor: just start typing. The bottom two lines list the shortcuts, where ^ means Ctrl and M- means Alt:
That's genuinely all you need to edit config files. A good habit before editing system files is to keep a backup copy:
vim: the editor that's everywhere#
vi (and its improved version vim, or the modern fork neovim) is installed on virtually every Unix-like system, including minimal servers and containers where nano might be missing. Many tools (git commit, crontab -e, visudo) may open it by default. Knowing the basics means you'll never be stuck.
The key idea: modes
vim is a modal editor. Keys do different things depending on the mode:
If you're ever confused, press Esc (twice is fine) to get back to normal mode.
Survival kit
Your first edit, step by step: vim hello.txt → press i → type Hello from vim → press Esc → type :wq → Enter. Done.
Moving around
Editing in normal mode
These are vim's superpower. Commands combine a verb with a motion and an optional count:
So d3w deletes three words and y$ copies to the end of the line. Once the grammar clicks, editing gets very fast.
Search and replace
Handy settings
Type these in command-line mode, or put them (without the :) in ~/.vimrc to make them permanent:
To learn properly, run vimtutor: a 30-minute interactive lesson built into vim.
Choosing your default editor#
Commands like crontab -e, git commit and sudoedit open the editor named in the EDITOR (or VISUAL) environment variable:
On Debian/Ubuntu, sudo update-alternatives --config editor sets the system default, and select-editor sets your own default for some tools.
Editing system files safely#
sudoedit /etc/file(orsudo -e) copies the file to a temporary location, opens your editor as your user, and writes it back as root when you save. It's safer than running a whole editor as root.visudofor/etc/sudoersandcrontab -efor cron jobs check your syntax before saving. Always use them instead of editing those files directly.- Validate before restarting services:
sudo nginx -t,sudo sshd -t,sudo apachectl configtest. A typo in sshd_config can lock you out of a server.
Other editors#
- micro: a modern terminal editor with familiar Ctrl+S / Ctrl+C / Ctrl+V keys (
sudo apt install micro). - emacs: the other legendary editor, endlessly extensible.
- VS Code Remote-SSH: edit files on a server from your desktop editor. It's great for development, but you'll still want nano or vim for quick fixes.
Common mistakes#
- Getting stuck in vim. Press
Esc, then type:q!and Enter to quit without saving, or:wqto save and quit. - Typing commands in Insert mode in vim, so
:wqends up as text in the file. PressEscfirst. - Editing system files without
sudo, then being unable to save. Usesudo nano /etc/file, or better,sudoedit /etc/file. - Breaking a config file and restarting the service blindly. Make a backup copy first (
sudo cp file file.bak) and use the program's config test (e.g.sudo nginx -t).
What's next#
You can now read and edit any file. Next: who's allowed to read and edit them, with permissions and ownership.
Check your understanding
Quick quiz
1.In nano, the help bar shows
^O Write Out. What does^mean?2.You opened a file in vim by accident and want to leave WITHOUT saving. What do you type?
3.In vim's normal mode, what does
dddo?
Finished reading?
Mark this lesson complete to track your progress.