Vim vs nano: which editor to use on a server
nano is the easier editor: you open a file and type, and the shortcuts you need are printed at the bottom of the screen. Vim takes a few minutes to get through the door (press i to type, Esc then :wq to save and quit) but is far faster for anything beyond a small change. vi is specified by POSIX and present on nearly every Unix-like system; nano is common but not everywhere. Use nano for the occasional config edit, and learn enough vi to never be stuck.
| vi / Vim | nano | |
|---|---|---|
| Start typing | Press i first; Esc to stop | Just type |
| Save | :w ↵ | Ctrl-o ↵ (or Ctrl-s) |
| Quit | :q ↵ (:q! throws changes away) | Ctrl-x |
| Search | / and the word, n for the next match | Ctrl-w (or Ctrl-f since nano 8.0) |
| Cut and paste a line | dd, then p | Ctrl-k, then Ctrl-u |
| Undo and redo | u and Ctrl-r | M-U and M-E (Alt+U, Alt+E) |
| Help on screen | None until you ask (:help) | Two help lines always shown; Ctrl-g for the full list |
| Where it is installed | vi is in POSIX; on practically every Unix-like system, including BusyBox systems | Common on Linux distributions, but not universal |
| Time to the basics | A few minutes for the survival keys | None: the keys are on screen |
| Room to grow | Operators, motions, text objects, macros, :s and :g over ranges | Search and replace, syntax highlighting, multiple files; deliberately simple |
What happens when you open each one
Open a file with nano and it behaves like most editors: the cursor is in the text and keys type. The bottom two lines list the most useful commands, where ^ means Ctrl and M- means Alt (Meta), so ^X Exit means Ctrl-x to leave. If you have changed the file, nano asks whether to save it on the way out. The nano manual calls it "a small and friendly text editor", and it started life as a free clone of Pico, the editor from the Pine mail client.
Open the same file with vi or vim and you are in Normal mode, where letters are commands rather than text. That is the source of the "how do I exit Vim" joke: typing a sentence runs a string of commands instead. Press Esc to be sure you are in Normal mode, then :q! and ↵ to quit without saving, or :wq to save and quit. The full escape guide is how to exit Vim.
The never-stuck kit
Five things that get you through any vi on any server. Type the keys from memory.
Which one will be on the server
vi is specified by POSIX (in its User Portability Utilities option) and has been part of Unix systems for decades. In practice you will find a vi on almost any Linux server, BSD or macOS machine; on Debian and Ubuntu it is often a small build called vim.tiny, and on BusyBox-based systems it is BusyBox's own vi, which covers the basics.
nano is installed on many Linux distributions and is easy to add, but it is not guaranteed. Very small container images may have neither editor, which is one reason it is worth knowing the vi basics: they work in whichever vi you end up in.
When nano is the right call
nano is a good choice when the edit is small and rare: changing one line in /etc/hosts, uncommenting an option in a config file, fixing a typo in a cron entry. Nobody on your team needs to have learned anything to do it safely, and the shortcuts on screen make mistakes unlikely. It is also a sensible default for shared servers where people of mixed experience log in.
Two options worth knowing:
nano -v fileopens a file read-only, for looking without risk.nano --modernbindingsswaps in the keys most desktop programs use: Ctrl-s to save, Ctrl-q to quit, Ctrl-z to undo, Ctrl-c and Ctrl-v to copy and paste.
When Vim pays off
Vim earns its learning time when you edit often, edit large files, or make the same change many times. A few examples of what takes one command in Vim:
:%s/old/new/greplaces every match in the file (find and replace).:g/DEBUG/ddeletes every line that mentions DEBUG.- ci" replaces the text inside the quotes the cursor is in, and . repeats that change wherever you go next.
- gg, G and
:120jump to the top, the bottom or line 120 of a long log.
You don't need all of Vim to be comfortable on a server. A survival set of i, Esc, :wq, :q!, dd, u, / and n covers most one-off edits, and the vi cheat sheet lists the keys that work even in a minimal vi.
Tighten an SSH config
A real server chore. Set the root-login option to no: j and $ reach the value, ciw changes it. Do the same on the next line with j, $ and .. Then move SSH to port 2222 with gg and A. On a real server you would save with :wq ↵ (and make sure your key login works first).
This editor needs a keyboard. Read on, and play it at your computer.
How to set your default editor
Programs that open an editor for you (git commit, crontab -e and many others) look at environment variables. Set both in your shell's startup file, for example ~/.bashrc or ~/.zshrc:
export EDITOR=nano(orvim)export VISUAL=nano
For Git alone, git config --global core.editor nano sets it. Git checks GIT_EDITOR, then core.editor, then VISUAL, then EDITOR, and falls back to the default it was built with, which is usually vi. That fallback is why a first git commit so often lands people in Vim.
On Debian and Ubuntu there are two more layers:
select-editorasks which installed editor you want and stores your choice in~/.selected_editor, used bysensible-editor.VISUALandEDITORstill win over it.sudo update-alternatives --config editorpicks the system-wideeditorcommand, the fallback for every user who hasn't chosen.
Which should you pick?
- You edit a config file over SSH once in a while nano. Its shortcuts are on screen and there is nothing to remember.
- You log in to many different machines, containers or appliances Learn the vi survival keys. vi is the editor you can count on finding.
- You edit files on servers every day Vim. Search, replace, repeat and jumping around large files repay the learning time quickly.
- You run servers that a mixed team logs in to Install both and let people set
EDITOR. nano is the friendlier system default.
Questions
- How do I save and exit in nano?
- Press Ctrl-x. If the file has changed, nano asks whether to save: press y, then ↵ to keep the file name. Ctrl-o saves without leaving.
- How do I get out of vi or Vim?
- Press Esc, then type
:q!and ↵ to quit without saving, or:wqto save and quit. - Is nano installed on every Linux system?
- No. It is common, but minimal systems and containers may not have it. Some form of vi is far more widely available.
- Why did git open Vim?
- Git uses
GIT_EDITOR,core.editor,VISUALorEDITOR, and if none is set it falls back to its built-in default, usually vi. Setcore.editororEDITORto the editor you prefer.