vimEdit files on any server with vim
vim is the editor you'll find on almost every Linux server and container image (often as vi). It's modal: keys are commands in Normal mode and text only in Insert mode. Knowing how to open, edit, search, replace, save and quit cleanly is a basic survival skill in interviews and on call.
vim [+LINE | +/PATTERN] [-R] FILE... sudoedit FILEYou need to change worker_processes in the nginx config on a server, with vim as the only editor.
vimVi IMproved. On minimal systems only vi (or vim.tiny) may exist; the basics are the same.
vim reads the whole file into an in-memory buffer and creates a swap file next to it (.nginx.conf.swp) where it records your changes, so a crashed session can be recovered and a second vim on the same file is warned. You start in Normal mode, where every key is a command (dd deletes a line, u undoes, /text searches). i, a or o switch to Insert mode, where keys become text; Esc goes back. : opens the command line for ex commands like :w (write), :q (quit), :%s/old/new/g (substitute on all lines) and :set number. On :w vim writes the buffer back, keeping owner and mode; depending on 'backupcopy' it overwrites the file in place or writes a new file and renames it over the old one. If you opened a file you can't write, vim sets 'readonly' and refuses :w (E45) unless forced, and even then the kernel will refuse without permission. The safest way to edit root-owned files is sudoedit, which runs your editor as you on a temporary copy and installs it with root rights when you quit.
vim starts in Normal mode where keys are commands; you enter Insert mode to type, the command line to save and quit, and Esc always brings you back.
vim loads the file into a buffer in memory and creates a .swp swap file. You land in Normal mode, where every key is a command, not text.
i / a / oInsert before cursor / after cursor / on a new line belowEscBack to Normal mode (press it whenever in doubt):w :q :wq :q!Write, quit, write and quit, quit discarding changesdd / yy / pDelete (cut) a line / copy a line / paste belowu / Ctrl+rUndo / redogg / G / :42First line / last line / line 42/text n NSearch forward, next match, previous match:%s/old/new/gReplace all occurrences in the file (add c to confirm each)v / VSelect characters / whole lines (then y, d, >):set number / :set pasteLine numbers / paste without auto-indent messvim -R / viewOpen read-onlysudoedit FILEEdit a root-owned file safely as yourselfTyping text right after opening vimYou're in Normal mode, so letters run as commands: d deletes, x cuts, u undoes, and the file is mangled.
Press i first; Esc when done typingClosing the terminal or killing SSH to escape vimLeaves a .swp file behind, and the next person (or you) gets the E325 ATTENTION screen.
Esc, then :q! (discard) or :wq (save)sudo vim /etc/... with your full personal .vimrc and pluginsRuns your editor and its plugins as root; any plugin bug or exploit runs with root rights, and files like viminfo may end up owned by root.
sudoedit /etc/file (sudo -e): you edit a temp copy as yourselfPasting YAML into vim and getting a staircase of indentationAuto-indent re-indents each pasted line, breaking YAML structure.
:set paste before pasting (:set nopaste after), or use a terminal with bracketed pasteTry these on a Linux machine of your own. Each task has a hint and a solution.
Open ~/app/config/app.yaml in vim, change workers: 4 to workers: 8, save and quit.
Open /etc/app/app.conf, turn on line numbers, jump to line 10, then quit without changing anything.
In a copy of ~/app/config/app.yaml, replace every 'db.internal' with 'db-new.internal' in one command and save.
Edit /etc/app/features.conf as root the safe way, setting beta_reports=on.
vimEdit files on any server with vim
vim is the editor you'll find on almost every Linux server and container image (often as vi). It's modal: keys are commands in Normal mode and text only in Insert mode. Knowing how to open, edit, search, replace, save and quit cleanly is a basic survival skill in interviews and on call.
vim [+LINE | +/PATTERN] [-R] FILE... sudoedit FILEYou need to change worker_processes in the nginx config on a server, with vim as the only editor.
vimVi IMproved. On minimal systems only vi (or vim.tiny) may exist; the basics are the same.
vim reads the whole file into an in-memory buffer and creates a swap file next to it (.nginx.conf.swp) where it records your changes, so a crashed session can be recovered and a second vim on the same file is warned. You start in Normal mode, where every key is a command (dd deletes a line, u undoes, /text searches). i, a or o switch to Insert mode, where keys become text; Esc goes back. : opens the command line for ex commands like :w (write), :q (quit), :%s/old/new/g (substitute on all lines) and :set number. On :w vim writes the buffer back, keeping owner and mode; depending on 'backupcopy' it overwrites the file in place or writes a new file and renames it over the old one. If you opened a file you can't write, vim sets 'readonly' and refuses :w (E45) unless forced, and even then the kernel will refuse without permission. The safest way to edit root-owned files is sudoedit, which runs your editor as you on a temporary copy and installs it with root rights when you quit.
vim starts in Normal mode where keys are commands; you enter Insert mode to type, the command line to save and quit, and Esc always brings you back.
vim loads the file into a buffer in memory and creates a .swp swap file. You land in Normal mode, where every key is a command, not text.
i / a / oInsert before cursor / after cursor / on a new line belowEscBack to Normal mode (press it whenever in doubt):w :q :wq :q!Write, quit, write and quit, quit discarding changesdd / yy / pDelete (cut) a line / copy a line / paste belowu / Ctrl+rUndo / redogg / G / :42First line / last line / line 42/text n NSearch forward, next match, previous match:%s/old/new/gReplace all occurrences in the file (add c to confirm each)v / VSelect characters / whole lines (then y, d, >):set number / :set pasteLine numbers / paste without auto-indent messvim -R / viewOpen read-onlysudoedit FILEEdit a root-owned file safely as yourselfTyping text right after opening vimYou're in Normal mode, so letters run as commands: d deletes, x cuts, u undoes, and the file is mangled.
Press i first; Esc when done typingClosing the terminal or killing SSH to escape vimLeaves a .swp file behind, and the next person (or you) gets the E325 ATTENTION screen.
Esc, then :q! (discard) or :wq (save)sudo vim /etc/... with your full personal .vimrc and pluginsRuns your editor and its plugins as root; any plugin bug or exploit runs with root rights, and files like viminfo may end up owned by root.
sudoedit /etc/file (sudo -e): you edit a temp copy as yourselfPasting YAML into vim and getting a staircase of indentationAuto-indent re-indents each pasted line, breaking YAML structure.
:set paste before pasting (:set nopaste after), or use a terminal with bracketed pasteTry these on a Linux machine of your own. Each task has a hint and a solution.
Open ~/app/config/app.yaml in vim, change workers: 4 to workers: 8, save and quit.
Open /etc/app/app.conf, turn on line numbers, jump to line 10, then quit without changing anything.
In a copy of ~/app/config/app.yaml, replace every 'db.internal' with 'db-new.internal' in one command and save.
Edit /etc/app/features.conf as root the safe way, setting beta_reports=on.