Editing Fundamentals
The Grammar of Editing
Neovim's editing model is often described as a language with a grammar:
[count] + operator + motion/text-object
This composability is what makes Neovim powerful.
Operators
| Operator | Action |
|---|---|
d | Delete (also cuts to register) |
y | Yank (copy to register) |
c | Change (delete and enter Insert mode) |
> | Indent right |
< | Indent left |
= | Auto-indent |
gU | Uppercase |
gu | Lowercase |
Text Objects
Text objects define a region of text. They work with any operator.
| Text Object | Meaning |
|---|---|
iw | Inner word |
aw | A word (includes surrounding whitespace) |
i" | Inner double-quoted string |
a" | A double-quoted string (includes quotes) |
i' | Inner single-quoted string |
i( or i) | Inner parenthesized block |
a( or a) | A parenthesized block (includes parens) |
i{ or i} | Inner brace block |
i[ or i] | Inner bracket block |
it | Inner XML/HTML tag |
at | A XML/HTML tag (includes tags) |
ip | Inner paragraph |
ap | A paragraph (includes blank line) |
is | Inner sentence |
as | A sentence |
Composing Examples
| Command | What It Does |
|---|---|
dw | Delete to end of word |
d$ | Delete to end of line |
dd | Delete entire line |
3dd | Delete 3 lines |
diw | Delete inner word (the word under cursor) |
dap | Delete a paragraph |
ci" | Change inner double-quoted string |
yi{| Yank inner brace block | |gUiw| Uppercase inner word |>ip| Indent inner paragraph |
Other Essential Editing Keys
| Key | Action |
|---|---|
x | Delete character under cursor |
X | Delete character before cursor |
p | Paste after cursor |
P | Paste before cursor |
u | Undo |
Ctrl-r | Redo |
. | Repeat last change |
~ | Toggle case of character |
J | Join current line with next line |
r{char} | Replace character under cursor with {char} |
The Dot Command
The . command is one of the most important keys in Neovim. It repeats the last change. Combined with search (n), this lets you make repetitive edits very efficiently:
/foo— search for "foo"cgnbar<Esc>— change the match to "bar"n— go to next match.— repeat the change- Repeat steps 3-4 as needed.
Search and Replace
Basic Search
/pattern " Search forward
?pattern " Search backward
n " Next match
N " Previous match
* " Search word under cursor (forward)
# " Search word under cursor (backward)
Search Options
:set ignorecase " Case-insensitive search
:set smartcase " Case-sensitive if pattern has uppercase
:set hlsearch " Highlight all matches
:set incsearch " Show matches as you type
:nohlsearch " Temporarily clear highlighting (shortcut: :noh)
Substitute (Find and Replace)
:s/old/new/ " Replace first occurrence on current line
:s/old/new/g " Replace all occurrences on current line
:%s/old/new/g " Replace all occurrences in entire file
:%s/old/new/gc " Replace all with confirmation
:5,10s/old/new/g " Replace on lines 5-10
Useful Patterns
:%s/\s\+$// " Remove trailing whitespace
:%s/\n\{3,}/\r\r/g " Collapse multiple blank lines to one
:%s/\<foo\>/bar/g " Replace whole word "foo" only (word boundaries)