Topic 1.6
Editing Files on a Server: nano and vim Survival
In one line
Servers rarely have a graphical editor. nano is the friendly option; vim is everywhere, including minimal images and recovery shells. Learn enough vim to open, edit, save, and, most importantly, quit.
Think of it like this
Vim is like a typewriter with two modes. In one mode your keys MOVE and COMMAND the page (delete a line, jump to the end); in the other they TYPE letters. Most vim confusion is simply being in the wrong mode.
Key ideas
- 01
NANO:
nano file.conf, type normally,Ctrl+Othen Enter to save,Ctrl+Xto exit,Ctrl+Wto search. The shortcuts are listed at the bottom of the screen. Perfect for quick config edits. - 02
VIM MODES: NORMAL mode (the default; keys are commands), INSERT mode (typing text; enter with
i, leave withEsc), and COMMAND-LINE mode (:then a command). Saving and quitting::wsave,:qquit,:wqsave and quit,:q!quit WITHOUT saving (the famous escape hatch). - 03
Normal-mode essentials:
h j k lor arrows to move,gg/Gtop/bottom,/textthennto search,dddelete a line,yycopy a line,ppaste,uundo,Ctrl+rredo,:%s/old/new/greplace everywhere,:set numbershow line numbers. - 04
Safety habits:
sudoedit /etc/nginx/nginx.conf(orsudo -e) edits a copy with your own editor settings and writes it back as root, safer thansudo vim. Validate configs before reloading (nginx -t,sshd -t,visudo -c). Set your default editor withexport EDITOR=vim(used bygit commit,crontab -e,visudo).
Code & diagrams
vim /etc/myapp/app.conf
/listen search for "listen", n = next match
i start typing (INSERT mode)
Esc back to NORMAL mode
dd u delete line, undo
:%s/8080/9090/g replace everywhere
:wq save and quit :q! quit without savingExplain it without notes
You opened vim, typed some text and now nothing works as expected. What happened and how do you get out safely?
Practice
Using vim only, change the port in a config file from 8080 to 9090 on every line, show line numbers, and save.
Done when you can
I can edit, save, and quit in both nano and vim
I use sudoedit and validate configs before reloading