← Back to overview

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

OperatorAction
dDelete (also cuts to register)
yYank (copy to register)
cChange (delete and enter Insert mode)
>Indent right
<Indent left
=Auto-indent
gUUppercase
guLowercase

Text Objects

Text objects define a region of text. They work with any operator.

Text ObjectMeaning
iwInner word
awA 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
itInner XML/HTML tag
atA XML/HTML tag (includes tags)
ipInner paragraph
apA paragraph (includes blank line)
isInner sentence
asA sentence

Composing Examples

CommandWhat It Does
dwDelete to end of word
d$Delete to end of line
ddDelete entire line
3ddDelete 3 lines
diwDelete inner word (the word under cursor)
dapDelete 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

KeyAction
xDelete character under cursor
XDelete character before cursor
pPaste after cursor
PPaste before cursor
uUndo
Ctrl-rRedo
.Repeat last change
~Toggle case of character
JJoin 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:

  1. /foo — search for "foo"
  2. cgnbar<Esc> — change the match to "bar"
  3. n — go to next match
  4. . — repeat the change
  5. Repeat steps 3-4 as needed.

Search and Replace

/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)