Skip to main content

Editing

Complete guide to text editing operations in Vim.

Insert Mode Operations

Entering Insert Mode

i    " Insert before cursor
a " Insert after cursor
I " Insert at beginning of line
A " Insert at end of line
o " Open new line below current line
O " Open new line above current line
s " Delete character under cursor and enter insert mode
S " Delete entire line and enter insert mode

Insert Mode Navigation

Ctrl+h    " Backspace (delete character before cursor)
Ctrl+w " Delete word before cursor
Ctrl+u " Delete line before cursor
Ctrl+t " Indent current line
Ctrl+d " Unindent current line

Insert Mode Completion

Ctrl+n    " Next completion
Ctrl+p " Previous completion
Ctrl+x Ctrl+n " Complete from current file
Ctrl+x Ctrl+f " Complete filename
Ctrl+x Ctrl+l " Complete line
Ctrl+x Ctrl+o " Omni completion

Delete Operations

Character Deletion

x    " Delete character under cursor
X " Delete character before cursor

Word Deletion

dw    " Delete word from cursor to end of word
db " Delete word from cursor to beginning of word
diw " Delete inner word (entire word)
daw " Delete a word (word + surrounding space)

Line Deletion

dd    " Delete entire line
d$ " Delete from cursor to end of line
D " Delete from cursor to end of line (same as d$)
d^ " Delete from cursor to first non-blank character
d0 " Delete from cursor to beginning of line

Advanced Deletion

dj    " Delete current line and line below
dk " Delete current line and line above
d3w " Delete 3 words
d5j " Delete 5 lines down
dG " Delete from cursor to end of file
dgg " Delete from cursor to beginning of file

Change Operations

Character Change

r{char}    " Replace character under cursor with {char}
R " Enter replace mode

Word Change

cw    " Change word from cursor to end of word
cb " Change word from cursor to beginning of word
ciw " Change inner word
caw " Change a word (word + surrounding space)

Line Change

cc    " Change entire line
C " Change from cursor to end of line
c$ " Change from cursor to end of line (same as C)
c^ " Change from cursor to first non-blank character
c0 " Change from cursor to beginning of line

Advanced Change

cj    " Change current line and line below
ck " Change current line and line above
c3w " Change 3 words
c5j " Change 5 lines down

Copy (Yank) Operations

Character/Word Yank

yl    " Yank character
yw " Yank word from cursor to end of word
yb " Yank word from cursor to beginning of word
yiw " Yank inner word
yaw " Yank a word (word + surrounding space)

Line Yank

yy    " Yank entire line
Y " Yank entire line (same as yy)
y$ " Yank from cursor to end of line
y^ " Yank from cursor to first non-blank character
y0 " Yank from cursor to beginning of line

Advanced Yank

yj    " Yank current line and line below
yk " Yank current line and line above
y3w " Yank 3 words
y5j " Yank 5 lines down
yG " Yank from cursor to end of file
ygg " Yank from cursor to beginning of file

Paste Operations

Basic Paste

p    " Paste after cursor/line
P " Paste before cursor/line

Register Paste

"0p    " Paste from register 0 (last yank)
"1p " Paste from register 1 (last delete)
"+p " Paste from system clipboard
"*p " Paste from primary selection

Paste with Formatting

]p    " Paste after cursor with auto-indent
[p " Paste before cursor with auto-indent
gp " Paste after cursor and move cursor after pasted text
gP " Paste before cursor and move cursor after pasted text

Text Objects

Inner Text Objects

iw    " Inner word
is " Inner sentence
ip " Inner paragraph
it " Inner tag (HTML/XML)
i" " Inner double quotes
i' " Inner single quotes
i` " Inner backticks
i( " Inner parentheses
i) " Inner parentheses (same as i()
i[ " Inner square brackets
i] " Inner square brackets (same as i[)
i{ " Inner curly braces
i} " Inner curly braces (same as i{)
i< " Inner angle brackets
i> " Inner angle brackets (same as i<)

Around Text Objects

aw    " Around word (includes surrounding space)
as " Around sentence
ap " Around paragraph
at " Around tag
a" " Around double quotes
a' " Around single quotes
a` " Around backticks
a( " Around parentheses
a) " Around parentheses (same as a()
a[ " Around square brackets
a] " Around square brackets (same as a[)
a{ " Around curly braces
a} " Around curly braces (same as a{)
a< " Around angle brackets
a> " Around angle brackets (same as a<)

Text Object Examples

diw    " Delete inner word
ci" " Change inside double quotes
yi( " Yank inside parentheses
va{ " Visual select around curly braces
da[ " Delete around square brackets

Indentation

Manual Indentation

>>    " Indent line
<< " Unindent line
> " Indent (in visual mode)
< " Unindent (in visual mode)

Automatic Indentation

=     " Auto-indent (in visual mode)
== " Auto-indent current line
=G " Auto-indent from cursor to end of file
gg=G " Auto-indent entire file

Indentation with Counts

3>>   " Indent 3 lines
5<< " Unindent 5 lines

Case Manipulation

Case Change

~     " Toggle case of character under cursor
g~ " Toggle case (use with motion)
gu " Convert to lowercase (use with motion)
gU " Convert to uppercase (use with motion)

Case Examples

g~iw   " Toggle case of inner word
guiw " Convert inner word to lowercase
gUiw " Convert inner word to uppercase
g~$ " Toggle case from cursor to end of line
guu " Convert current line to lowercase
gUU " Convert current line to uppercase

Joining Lines

Line Joining

J     " Join current line with next line
gJ " Join lines without inserting space

Multiple Line Joining

3J    " Join 3 lines
VjjJ " Visual select 3 lines and join

Repetition

Repeat Commands

.     " Repeat last command
@@ " Repeat last macro

Repeat with Counts

3.    " Repeat last command 3 times
5@@ " Repeat last macro 5 times

Undo and Redo

Basic Undo/Redo

u       " Undo last change
Ctrl+r " Redo last undo
U " Undo all changes on current line

Advanced Undo

g-      " Go to older text state
g+ " Go to newer text state
:earlier 5m " Go to text state 5 minutes ago
:later 10s " Go to text state 10 seconds later

Undo Branches

:undolist    " Show undo list
:undo 5 " Go to undo state 5

Advanced Editing Techniques

Increment/Decrement Numbers

Ctrl+a    " Increment number under cursor
Ctrl+x " Decrement number under cursor

Multiple Cursors (Visual Block)

Ctrl+v    " Enter visual block mode
I " Insert at beginning of block
A " Insert at end of block
c " Change block

Sorting

:sort     " Sort selected lines
:sort! " Sort in reverse order
:sort u " Sort and remove duplicates

Commenting (with appropriate plugins)

gc    " Toggle comment (with commentary plugin)
gcc " Toggle comment for current line

Practical Editing Workflows

Common Editing Patterns

ciw    " Change inner word
di" " Delete inside quotes
yi( " Yank inside parentheses
va{ " Visual select around braces
da[ " Delete around brackets

Efficient Text Manipulation

dwi    " Delete word and insert
caw " Change a word (including spaces)
dap " Delete a paragraph
yap " Yank a paragraph

Quick Fixes

xp     " Swap two characters
ddp " Swap two lines
yyp " Duplicate line

Editing with Registers

Named Registers

"ayy   " Yank line to register 'a'
"ap " Paste from register 'a'
"Ayy " Append line to register 'a'

Special Registers

"0     " Last yank register
"1-9 " Delete registers
"" " Default register
"+ " System clipboard
"* " Primary selection
"/ " Last search pattern
": " Last command

Tips for Efficient Editing

Workflow Tips

  1. Use text objects for precise selections
  2. Combine operators with motions for powerful editing
  3. Learn to use the dot command for repetition
  4. Master visual block mode for column editing
  5. Use registers for multiple clipboard operations

Common Mistakes to Avoid

  1. Don't stay in insert mode - return to normal mode frequently
  2. Don't use arrow keys - use hjkl for better efficiency
  3. Don't repeat commands manually - use counts and dot command
  4. Don't forget about text objects - they're more precise than motions

This comprehensive editing guide covers all the essential operations for efficient text manipulation in Vim. Practice these commands to become proficient in Vim's powerful editing capabilities.