The minimum Vim commands for editing config files on a server
A table of the Vim keys and commands you need to open, edit, save, and quit config files when you SSH into a server.
Most Linux servers come with vi/vim preinstalled, while nano isn’t always available. To edit a config file, you only need the few commands below.
Vim has two main modes: normal mode for moving around and typing commands, and insert mode for typing text. If you’re not sure which mode you’re in, press Esc to go back to normal mode.
| Key | What it does |
|---|---|
vim /etc/nginx/nginx.conf | Open a file (if you lack permission, use sudo vim ...) |
i / o | Enter insert mode at the cursor / open a new line below |
Esc | Go back to normal mode |
gg / G / :42 | Jump to the top of the file / the bottom of the file / line 42 |
/listen then n, N | Search for “listen”, go to the next / previous match |
x | Delete one character (handy for removing a # when re-enabling a line) |
dd | Delete (cut) the whole line |
yy / p | Copy a line / paste it below |
u / Ctrl+r | Undo / redo |
:%s/80/8080/g | Replace every “80” with “8080” in the file |
:set number | Show line numbers, handy when a log reports an error on a specific line |
:set paste | Turn on before pasting from the clipboard so Vim doesn’t auto-indent incorrectly |
:w | Save |
:wq or :x or ZZ | Save and quit |
:q! | Quit and discard all changes |
:e! | Discard changes but stay in the editor, reloading the file from disk |
:w !sudo tee % | You opened a system file but forgot sudo: write it through sudo tee |
A few notes:
- If you type
:qand Vim reportsE37: No write since last change, you have unsaved edits. To keep them, use:wq; to discard them, use:q!. :%suses regex, so:%s/80/8080/gwill also turn an existing8080into80808080. To be safer, use:%s/\<80\>/8080/gor addcat the end (/gc) so Vim asks for confirmation at each match.- After
:w !sudo tee %, Vim will warn that the file has changed outside the editor. PressLto reload it, then:qto quit.
Vim doesn’t check the syntax of config files, so after saving, remember to run the corresponding check command (for example nginx -t) before reloading the service.