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:

  • Normal mode - Navigation and commands (default)

  • Insert mode - Typing text

  • Visual mode - Selecting text

  • Command mode - Ex commands (: prefix)

Press Esc to return to Normal mode from any other mode.

2. Variants

Editor Description Common On

vi

Original, minimal

All Unix/Linux, rescue modes

vim

Vi IMproved, feature-rich

Most Linux distros

nvim / neovim

Modern vim fork, Lua config

Developer workstations

3. Starting and Exiting

3.1. Opening Files

# Open file
vim /etc/ssh/sshd_config

# Open at specific line
vim +25 /etc/ssh/sshd_config

# Open at pattern
vim +/PasswordAuthentication /etc/ssh/sshd_config

# Open multiple files
vim file1.txt file2.txt

3.2. Exiting

Command Action

:q

Quit (fails if unsaved changes)

:q!

Quit without saving (force)

:w

Write (save)

:wq or :x

Write and quit

ZZ

Write and quit (Normal mode)

ZQ

Quit without saving (Normal mode)

4. Modes

4.1. Enter Insert Mode

Key Action

i

Insert before cursor

I

Insert at beginning of line

a

Append after cursor

A

Append at end of line

o

Open new line below

O

Open new line above

s

Substitute character (delete and insert)

S

Substitute line

4.2. Exit Insert Mode

Press Esc to return to Normal mode.

4.3. Enter Visual Mode

Key Action

v

Character-wise visual

V

Line-wise visual

Ctrl+v

Block visual (column select)

5. Navigation

5.1. Basic Movement (Normal Mode)

Key Movement

h

Left

j

Down

k

Up

l

Right

w

Next word

b

Previous word

e

End of word

0

Beginning of line

^

First non-blank character

$

End of line

5.2. Screen Movement

Key Movement

gg

Go to first line

G

Go to last line

{n}G or :{n}

Go to line n

Ctrl+f

Page forward (down)

Ctrl+b

Page backward (up)

Ctrl+d

Half page down

Ctrl+u

Half page up

H

Top of screen

M

Middle of screen

L

Bottom of screen

zz

Center cursor line on screen

6. Editing

6.1. Delete

Key Action

x

Delete character under cursor

X

Delete character before cursor

dw

Delete word

dd

Delete line

d$ or D

Delete to end of line

d0

Delete to beginning of line

dG

Delete to end of file

dgg

Delete to beginning of file

{n}dd

Delete n lines

6.2. Copy (Yank) and Paste

Key Action

yy or Y

Yank (copy) line

yw

Yank word

y$

Yank to end of line

{n}yy

Yank n lines

p

Paste after cursor

P

Paste before cursor

6.3. Undo and Redo

Key Action

u

Undo

Ctrl+r

Redo

U

Undo all changes on line

6.4. Change (Delete + Insert)

Key Action

cw

Change word

cc or S

Change entire line

c$ or C

Change to end of line

ci"

Change inside quotes

ci(

Change inside parentheses

7. Search and Replace

Key Action

/pattern

Search forward

?pattern

Search backward

n

Next match

N

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
Example: Update config value
:%s/PasswordAuthentication yes/PasswordAuthentication no/g
Example: Comment out a line
: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

8.2. Windows (Splits)

:split file           " Horizontal split
:vsplit file          " Vertical split
Ctrl+w w              " Switch windows
Ctrl+w h/j/k/l        " Move to window
Ctrl+w =              " Equal size windows
Ctrl+w q              " Close window

8.3. Tabs

:tabnew file          " Open in new tab
:tabn                 " Next tab
:tabp                 " Previous tab
gt                    " Next tab (Normal mode)
gT                    " Previous tab
:tabclose             " Close tab

9. System Administration Tasks

9.1. View File Without Editing

# Read-only mode
vim -R /etc/passwd
view /etc/passwd

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

9.3. Compare Files (vimdiff)

vimdiff file1 file2
vim -d file1 file2

9.4. Quick Configuration Edits

Disable Password Authentication in SSH
sudo vim /etc/ssh/sshd_config
# Then in vim:
/PasswordAuthentication
# Change yes to no
:wq

9.5. View Large Log Files

# Open at end of file
vim + /var/log/syslog

# Open and go to last line
vim /var/log/syslog
G

10. Command Mode Operations

10.1. Line Numbers

:set number           " Show line numbers
:set nonumber         " Hide line numbers
:set relativenumber   " Relative line numbers

10.2. Syntax Highlighting

:syntax on
:syntax off
:set filetype=bash    " Force filetype

10.3. Indentation

:set tabstop=4        " Tab width
:set shiftwidth=4     " Indent width
:set expandtab        " Spaces instead of tabs
:set autoindent       " Auto indent

10.4. Show Hidden Characters

:set list             " Show tabs, trailing spaces
:set nolist           " Hide

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
Example: Comment multiple lines
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

12.2. Clipboard Integration

" Use system clipboard
:set clipboard=unnamedplus

" In Lua (init.lua)
vim.opt.clipboard = "unnamedplus"

12.3. Terminal Mode

:terminal             " Open terminal
Ctrl+\ Ctrl+n         " Exit terminal mode

13. Minimal vi (Rescue Mode)

When on minimal systems or in rescue mode, only basic vi is available.

13.1. Essential Commands

i           " Insert mode
Esc         " Normal mode
:wq         " Save and quit
:q!         " Quit without saving
dd          " Delete line
yy          " Copy line
p           " Paste
/text       " Search
n           " Next match

14. Quick Reference Card

Mode Key Action

Any

Esc

Return to Normal mode

Normal

i

Insert mode

Normal

dd

Delete line

Normal

yy

Copy line

Normal

p

Paste

Normal

u

Undo

Normal

/text

Search

Normal

:wq

Save and quit

Normal

:q!

Quit without saving

Normal

gg

Go to top

Normal

G

Go to bottom

Normal

:%s/old/new/g

Replace all

Visual

V

Select lines

Visual

d

Delete selection

15. Cheat Sheet: Common Tasks

Task Commands

Open file at line 50

vim +50 file

Search and replace all

:%s/old/new/g

Delete lines 10-20

:10,20d

Copy lines 5-10

:5,10y

Indent selection

V select, then >

Comment lines

Ctrl+v, select, I#, Esc

Save as root

:w !sudo tee %

Compare files

vimdiff file1 file2

Show line numbers

:set number

Go to definition

gd (with LSP in neovim)

  • Vim Essentials (linux-ops) - Comprehensive vim reference (text objects, registers, macros, production scenarios)

  • Shell Patterns (linux-ops) - Heredoc, parameter expansion, arrays

  • sed Operations

  • Git Commands