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.

2 min read

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.

KeyWhat it does
vim /etc/nginx/nginx.confOpen a file (if you lack permission, use sudo vim ...)
i / oEnter insert mode at the cursor / open a new line below
EscGo back to normal mode
gg / G / :42Jump to the top of the file / the bottom of the file / line 42
/listen then n, NSearch for “listen”, go to the next / previous match
xDelete one character (handy for removing a # when re-enabling a line)
ddDelete (cut) the whole line
yy / pCopy a line / paste it below
u / Ctrl+rUndo / redo
:%s/80/8080/gReplace every “80” with “8080” in the file
:set numberShow line numbers, handy when a log reports an error on a specific line
:set pasteTurn on before pasting from the clipboard so Vim doesn’t auto-indent incorrectly
:wSave
:wq or :x or ZZSave 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 :q and Vim reports E37: No write since last change, you have unsaved edits. To keep them, use :wq; to discard them, use :q!.
  • :%s uses regex, so :%s/80/8080/g will also turn an existing 8080 into 80808080. To be safer, use :%s/\<80\>/8080/g or add c at 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. Press L to reload it, then :q to 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.