CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / vim

vim

Edit files on any server with vim

Intermediateeditoressentialconfig

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.

Syntaxvim [+LINE | +/PATTERN] [-R] FILE... sudoedit FILE

Watch it run

You need to change worker_processes in the nginx config on a server, with vim as the only editor.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
sudo vim /etc/nginx/nginx.conf

vim shows the file and, on the last line, its name, line and byte count and the cursor position (1,1). The ~ lines are past the end of the file. You are in Normal mode: keys are commands, not text.

Anatomy of a command

$
commandvim

Vi IMproved. On minimal systems only vi (or vim.tiny) may exist; the basics are the same.

How it works

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 filei a oEscv VEsc::w:qdd x p uShellvim nginx.confNORMALkeys = commandsstart hereINSERTkeys = textVISUALselect textCommand line:w :q :%s /Buffer + swapedits in memorynginx.conffile on disk
1/8
vim opens in Normal mode

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.

← → to step

Options you'll actually use

i / a / oInsert before cursor / after cursor / on a new line below
EscBack to Normal mode (press it whenever in doubt)
:w :q :wq :q!Write, quit, write and quit, quit discarding changes
dd / yy / pDelete (cut) a line / copy a line / paste below
u / Ctrl+rUndo / redo
gg / 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 mess
vim -R / viewOpen read-only
sudoedit FILEEdit a root-owned file safely as yourself

Common mistakes

Typing text right after opening vim

You'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 typing
Closing the terminal or killing SSH to escape vim

Leaves 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 plugins

Runs 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 yourself
Pasting YAML into vim and getting a staircase of indentation

Auto-indent re-indents each pasted line, breaking YAML structure.

:set paste before pasting (:set nopaste after), or use a terminal with bracketed paste

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Open ~/app/config/app.yaml in vim, change workers: 4 to workers: 8, save and quit.

  2. 2

    Open /etc/app/app.conf, turn on line numbers, jump to line 10, then quit without changing anything.

  3. 3

    In a copy of ~/app/config/app.yaml, replace every 'db.internal' with 'db-new.internal' in one command and save.

  4. 4

    Edit /etc/app/features.conf as root the safe way, setting beta_reports=on.

Related commands

nanoEdit files with nano, the beginner-friendly editorlessBrowse files and output: scroll, search, filter, followsedStream editor: find and replace, delete and print linessudoRun a command as root or another user
PreviouslessNextnano
Linux / File Viewing & Editing / vim

vim

Edit files on any server with vim

Intermediateeditoressentialconfig

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.

Syntaxvim [+LINE | +/PATTERN] [-R] FILE... sudoedit FILE

Watch it run

You need to change worker_processes in the nginx config on a server, with vim as the only editor.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
sudo vim /etc/nginx/nginx.conf

vim shows the file and, on the last line, its name, line and byte count and the cursor position (1,1). The ~ lines are past the end of the file. You are in Normal mode: keys are commands, not text.

Anatomy of a command

$
commandvim

Vi IMproved. On minimal systems only vi (or vim.tiny) may exist; the basics are the same.

How it works

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 filei a oEscv VEsc::w:qdd x p uShellvim nginx.confNORMALkeys = commandsstart hereINSERTkeys = textVISUALselect textCommand line:w :q :%s /Buffer + swapedits in memorynginx.conffile on disk
1/8
vim opens in Normal mode

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.

← → to step

Options you'll actually use

i / a / oInsert before cursor / after cursor / on a new line below
EscBack to Normal mode (press it whenever in doubt)
:w :q :wq :q!Write, quit, write and quit, quit discarding changes
dd / yy / pDelete (cut) a line / copy a line / paste below
u / Ctrl+rUndo / redo
gg / 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 mess
vim -R / viewOpen read-only
sudoedit FILEEdit a root-owned file safely as yourself

Common mistakes

Typing text right after opening vim

You'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 typing
Closing the terminal or killing SSH to escape vim

Leaves 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 plugins

Runs 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 yourself
Pasting YAML into vim and getting a staircase of indentation

Auto-indent re-indents each pasted line, breaking YAML structure.

:set paste before pasting (:set nopaste after), or use a terminal with bracketed paste

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Open ~/app/config/app.yaml in vim, change workers: 4 to workers: 8, save and quit.

  2. 2

    Open /etc/app/app.conf, turn on line numbers, jump to line 10, then quit without changing anything.

  3. 3

    In a copy of ~/app/config/app.yaml, replace every 'db.internal' with 'db-new.internal' in one command and save.

  4. 4

    Edit /etc/app/features.conf as root the safe way, setting beta_reports=on.

Related commands

nanoEdit files with nano, the beginner-friendly editorlessBrowse files and output: scroll, search, filter, followsedStream editor: find and replace, delete and print linessudoRun a command as root or another user
PreviouslessNextnano