Vim / Neovim Reference
1. Overview
vi, vim, and neovim are modal text editors available on virtually every Unix/Linux system. Essential for editing configuration files, especially on headless servers.
|
Modal Editing Vim has distinct modes:
Press |
2. Variants
| Editor | Description | Common On |
|---|---|---|
|
Original, minimal |
All Unix/Linux, rescue modes |
|
Vi IMproved, feature-rich |
Most Linux distros |
|
Modern vim fork, Lua config |
Developer workstations |
3. Starting and Exiting
4. Modes
5. Navigation
6. Editing
6.1. Delete
| Key | Action |
|---|---|
|
Delete character under cursor |
|
Delete character before cursor |
|
Delete word |
|
Delete line |
|
Delete to end of line |
|
Delete to beginning of line |
|
Delete to end of file |
|
Delete to beginning of file |
|
Delete n lines |
7. Search and Replace
7.1. Search
| Key | Action |
|---|---|
|
Search forward |
|
Search backward |
|
Next match |
|
Previous match |
|
Search forward for word under cursor |
|
Search backward for word under cursor |
7.2. Replace (Substitution)
:s/old/new/ " Replace first on current line
:s/old/new/g " Replace all on current line
:%s/old/new/g " Replace all in file
:%s/old/new/gc " Replace all with confirmation
:5,10s/old/new/g " Replace in lines 5-10
:%s/PasswordAuthentication yes/PasswordAuthentication no/g
:s/^/#/
8. Working with Multiple Files
8.1. Buffers
:ls " List buffers
:bn " Next buffer
:bp " Previous buffer
:b\{n\} " Go to buffer n
:bd " Delete (close) buffer
9. System Administration Tasks
9.2. Edit with Sudo
# Open as root
sudo vim /etc/ssh/sshd_config
# Save file opened without sudo (from within vim)
:w !sudo tee % > /dev/null
10. Command Mode Operations
11. Visual Mode Operations
11.1. Select and Operate
V " Select line
{motion} " Extend selection
d " Delete selection
y " Yank selection
> " Indent selection
< " Unindent selection
: " Command on selection
11.2. Block Selection (Column Edit)
Ctrl+v " Start block select
{motion} " Extend block
I " Insert before block
A " Append after block
d " Delete block
r " Replace block with char
Ctrl+v " Block select
jjj " Select lines
I " Insert mode
# " Type comment char
Esc " Apply to all lines
12. Neovim Specifics
12.1. Configuration
Neovim uses ~/.config/nvim/init.lua (Lua) or init.vim (Vimscript).
# Config location
~/.config/nvim/init.lua # Lua config
~/.config/nvim/init.vim # Legacy vimscript
# Check health
:checkhealth
14. Quick Reference Card
| Mode | Key | Action |
|---|---|---|
Any |
|
Return to Normal mode |
Normal |
|
Insert mode |
Normal |
|
Delete line |
Normal |
|
Copy line |
Normal |
|
Paste |
Normal |
|
Undo |
Normal |
|
Search |
Normal |
|
Save and quit |
Normal |
|
Quit without saving |
Normal |
|
Go to top |
Normal |
|
Go to bottom |
Normal |
|
Replace all |
Visual |
|
Select lines |
Visual |
|
Delete selection |
15. Cheat Sheet: Common Tasks
| Task | Commands |
|---|---|
Open file at line 50 |
|
Search and replace all |
|
Delete lines 10-20 |
|
Copy lines 5-10 |
|
Indent selection |
|
Comment lines |
|
Save as root |
|
Compare files |
|
Show line numbers |
|
Go to definition |
|