Skip to main content

vi/vim

November 10, 2025

Essential vi/vim editor commands

Essential commands for vi/vim text editor.

Modes

Switching Modes

# Enter insert mode (before cursor)
i

# Enter insert mode (after cursor)
a

# Enter insert mode (beginning of line)
I

# Enter insert mode (end of line)
A

# Enter insert mode (new line below)
o

# Enter insert mode (new line above)
O

# Return to normal mode
ESC

# Enter command mode
:

# Enter visual mode (character selection)
v

# Enter visual line mode (line selection)
V

# Enter visual block mode (column selection)
Ctrl+v

Basic Movement

# Move left
h

# Move down
j

# Move up
k

# Move right
l

# Move to beginning of line
0

# Move to first non-blank character
^

# Move to end of line
$

# Move forward one word
w

# Move backward one word
b

# Move to end of word
e

# Move to beginning of file
gg

# Move to end of file
G

# Move to line number (e.g., line 50)
:50 or 50G

# Move to matching bracket/parenthesis
%

Screen Navigation

# Move to top of screen
H

# Move to middle of screen
M

# Move to bottom of screen
L

# Scroll down half screen
Ctrl+d

# Scroll up half screen
Ctrl+u

# Scroll down full screen
Ctrl+f

# Scroll up full screen
Ctrl+b

# Center screen on cursor
zz

# Move screen so cursor is at top
zt

# Move screen so cursor is at bottom
zb

Editing

Delete

# Delete character under cursor
x

# Delete character before cursor
X

# Delete current line
dd

# Delete from cursor to end of line
D

# Delete word
dw

# Delete to end of word
de

# Delete to beginning of line
d0

# Delete inside word (cursor can be anywhere in word)
diw

# Delete inside parentheses
di(  or  di)

# Delete inside quotes
di"  or  di'

# Delete 5 lines
5dd

Copy (Yank)

# Yank current line
yy

# Yank word
yw

# Yank to end of line
y$

# Yank 3 lines
3yy

# Yank inside word
yiw

# Yank inside parentheses
yi(

# Yank entire file
:%y

Paste

# Paste after cursor
p

# Paste before cursor
P

Change

# Change word
cw

# Change to end of line
C

# Change entire line
cc

# Change inside word
ciw

# Change inside parentheses
ci(

# Change inside quotes
ci"

Undo/Redo

# Undo
u

# Redo
Ctrl+r

# Undo all changes on line
U

Replace

# Replace single character
r{char}

# Enter replace mode
R

Search and Replace

# Search forward
/{pattern}

# Search backward
?{pattern}

# Next match
n

# Previous match
N

# Search for word under cursor (forward)
*

# Search for word under cursor (backward)
#

# Clear search highlighting
:noh

Replace

# Replace first occurrence on current line
:s/old/new/

# Replace all occurrences on current line
:s/old/new/g

# Replace all occurrences in file
:%s/old/new/g

# Replace all occurrences with confirmation
:%s/old/new/gc

# Replace in visual selection
:'<,'>s/old/new/g

# Replace whole words only
:%s/\<old\>/new/g

File Operations

Saving and Quitting

# Save file
:w

# Save and quit
:wq  or  :x  or  ZZ

# Quit without saving
:q!  or  ZQ

# Save as new filename
:w newfile.txt

# Save lines 10-20 to new file
:10,20w newfile.txt

# Force write (override read-only)
:w!

File Management

# Edit new file
:e filename

# Reload current file from disk
:e!

# Insert contents of another file
:r filename

# Insert output of command
:r !ls

# Browse files
:browse e

# Show current filename
:f  or  Ctrl+g

Multiple Files

Buffers

# List buffers
:ls  or  :buffers

# Next buffer
:bn

# Previous buffer
:bp

# Switch to buffer number
:b2

# Delete buffer
:bd

# Switch to last buffer
:b#

Windows (Splits)

# Split horizontal
:sp  or  Ctrl+w s

# Split vertical
:vsp  or  Ctrl+w v

# Close window
:q  or  Ctrl+w q

# Close all but current
:only  or  Ctrl+w o

# Move between windows
Ctrl+w h/j/k/l

# Move to next window
Ctrl+w w

# Resize window larger
Ctrl+w +

# Resize window smaller
Ctrl+w -

# Equal size windows
Ctrl+w =

# Maximize current window
Ctrl+w _

Tabs

# Open new tab
:tabnew

# Next tab
:tabn  or  gt

# Previous tab
:tabp  or  gT

# Close tab
:tabc

# List tabs
:tabs

# Go to tab number
:tabn 3  or  3gt

Visual Mode

Selection

# Select characters
v

# Select lines
V

# Select block
Ctrl+v

# Select all
ggVG

# Reselect last selection
gv

Operations on Selection

# Delete selection
d

# Yank selection
y

# Change selection
c

# Indent selection
>

# Unindent selection
<

# Convert to uppercase
U

# Convert to lowercase
u

# Comment/uncomment (with commentary plugin)
gc

Macros

Recording and Playing

# Start recording macro in register 'a'
qa

# Stop recording
q

# Play macro from register 'a'
@a

# Repeat last macro
@@

# Play macro 10 times
10@a

Marks

Setting and Jumping

# Set mark 'a' at current position
ma

# Jump to mark 'a'
'a

# Jump to exact position of mark 'a'
`a

# Jump to last position before jump
''  or  ``

# List all marks
:marks

# Jump to last edit position
`.

Indentation

Indent Commands

# Indent line
>>

# Unindent line
<<

# Indent in insert mode
Ctrl+t

# Unindent in insert mode
Ctrl+d

# Indent selection (visual mode)
>

# Auto-indent current line
==

# Auto-indent entire file
gg=G

# Indent 5 lines
5>>

Advanced

Registers

# Show all registers
:reg

# Yank to register 'a'
"ayy

# Paste from register 'a'
"ap

# System clipboard (copy)
"+y

# System clipboard (paste)
"+p

# Last search pattern
"/

# File name
"%

Command Line

# Execute shell command
:!ls

# Insert command output
:r !date

# Filter lines through command
:%!sort

# Run command on range
:10,20!sort

Completion (Insert Mode)

# Word completion
Ctrl+n  or  Ctrl+p

# Line completion
Ctrl+x Ctrl+l

# File path completion
Ctrl+x Ctrl+f

# Dictionary completion
Ctrl+x Ctrl+k

# Complete from tags
Ctrl+x Ctrl+]

Configuration

Common Settings

# Show line numbers
:set number  or  :set nu

# Show relative line numbers
:set relativenumber  or  :set rnu

# Enable syntax highlighting
:syntax on

# Set tab width
:set tabstop=4

# Set indent width
:set shiftwidth=4

# Use spaces instead of tabs
:set expandtab

# Enable mouse support
:set mouse=a

# Show matching brackets
:set showmatch

# Highlight search results
:set hlsearch

# Incremental search
:set incsearch

# Case insensitive search
:set ignorecase

# Smart case (case sensitive if pattern contains uppercase)
:set smartcase

# Enable line wrapping
:set wrap

# Disable line wrapping
:set nowrap

# Show current mode
:set showmode

# Enable auto-indenting
:set autoindent

# Enable smart indenting
:set smartindent

# Save settings to vimrc
:mkvimrc ~/.vimrc

Quick Tips

Repeat Actions

# Repeat last command
.

# Repeat 10 times
10.

# Repeat last search
n

# Repeat last substitute
&

Text Objects

# Inner word (diw = delete inner word)
iw

# Around word (daw = delete around word)
aw

# Inner sentence
is

# Around sentence
as

# Inner paragraph
ip

# Around paragraph
ap

# Inner tag block (HTML/XML)
it

# Around tag block
at

Useful Combinations

# Delete line and enter insert mode
S  or  cc

# Join current line with next
J

# Change case of character
~

# Increment number under cursor
Ctrl+a

# Decrement number under cursor
Ctrl+x

# Format/wrap text to textwidth
gq

# Open URL under cursor (netrw)
gx