Nvim Session 04: Search Motions
Search isn’t just for finding - it’s a motion. Delete to a pattern, change until a match, navigate by regex.
Pre-Session State
-
Know
f/tfor character search -
Understand operators
-
Use
/search as a motion -
Know word-under-cursor search
Lesson 1: Basic Search
Exercise 1.1: Forward and backward search
/pattern " Search forward
?pattern " Search backward
n " Next match (same direction)
N " Next match (opposite direction)
Open a config file and try:
/hostname " Find hostname
n " Next hostname
N " Previous hostname
?server " Search backward for server
Exercise 1.2: Case sensitivity
/Hostname " Case sensitive (by default)
/hostname\c " Force case insensitive
/HOSTNAME\C " Force case sensitive
Or set defaults:
:set ignorecase " Always ignore case
:set smartcase " Ignore unless uppercase used
Exercise 1.3: Regex patterns
/^server " Lines starting with server
/\.dev$ " Lines ending with .dev
/10\.50\.[0-9]+ " IP pattern (escape dots!)
/\d\+ " One or more digits
/\w\+ " One or more word chars
Note: Vim regex uses + not + for "one or more".
Lesson 2: Word Under Cursor
Concept: Instantly search for the word you’re on.
Exercise 2.1: Exact word match
Position cursor on hostname:
server_hostname = value
|
* " Search forward for exact word
# " Search backward for exact word
Exact: Matches hostname but not hostnames or my_hostname.
Exercise 2.2: Partial word match
g* " Search forward (partial match)
g# " Search backward (partial match)
g* on host matches host, hostname, localhost.
Exercise 2.3: Staying in place
* " Jumps to NEXT match
# " Jumps to PREVIOUS match
To highlight without moving:
*N " Search forward, then back
#N " Search backward, then back
Or use:
:let @/ = expand('<cword>') " Set search register to current word
Lesson 3: Search as Motion
Concept: Search is a motion - combine with operators!
Exercise 3.1: Delete to pattern
server: vault-01.inside.domusdigitalis.dev:8200 |
d/\.dev " Delete up to .dev → .dev:8200
d/: " Delete up to colon → :8200
Exercise 3.2: Change to pattern
error_message = "Connection failed: timeout after 30s"
|
c/timeout " Change up to "timeout"
New message: | (in insert mode)
Exercise 3.3: Yank to pattern
log_line = "2026-03-18 14:30:22 ERROR Something went wrong"
|
y/ERROR " Yank up to ERROR
p " Paste it
Lesson 4: Search Navigation Patterns
Exercise 4.1: Navigate to next occurrence
/function " First occurrence
n " Next
n " Next
5n " Skip ahead 5 matches
Exercise 4.2: Count matches
:%s/pattern//gn " Count matches (n = no replace)
Exercise 4.3: Search history
/ " Start search
<Up> " Previous search
<Down> " Next in history
q/ " Open search history window
Lesson 5: Search and Replace Integration
Exercise 5.1: Search then substitute
/old_name " Find it first
cgn " Change next match (special motion!)
new_name<Esc>
. " Repeat (change next match)
. " And again...
gn is a motion to the next search match - powerful for interactive replace.
Exercise 5.2: Preview before replace
:%s/old/new/gc " c = confirm each
" y = yes, n = no, a = all, q = quit
Exercise 5.3: Replace in range
:10,20s/old/new/g " Lines 10-20
:'<,'>s/old/new/g " Visual selection
:.,$s/old/new/g " Current line to end
Lesson 6: Practical Search Patterns
Exercise 6.1: Config file navigation
[section1] key1 = value1 [section2] key2 = value2 [section3] key3 = value3
/^\[ " Jump to next section
?^\[ " Jump to previous section
/^key " Jump to next key
Exercise 6.2: Log file navigation
/ERROR\|WARN " Find errors or warnings
/\d\{4}-\d\{2}-\d\{2} " Find dates (YYYY-MM-DD)
/\<failed\> " Exact word "failed"
Exercise 6.3: Code navigation
/^function\|^def " Function definitions
/^class " Class definitions
/TODO\|FIXME " Find todos
Exercise 6.4: Delete to search
# This is a comment actual_code_starts_here
/^[^#]<CR> " First non-comment line
d?^#<CR> " Delete back to comment
Summary: Search Commands
| Command | Effect |
|---|---|
|
Search forward |
|
Search backward |
|
Next match (same direction) |
|
Next match (opposite) |
|
Word under cursor (forward, exact) |
|
Word under cursor (backward, exact) |
|
Partial word forward |
|
Partial word backward |
|
Motion to next match (for operators) |
|
Motion to previous match |
Search as Motion Reference
| Command | Effect |
|---|---|
|
Delete to pattern |
|
Change to pattern |
|
Yank to pattern |
|
Visual to pattern |
|
Change next search match |
|
Delete next search match |
Useful Settings
:set incsearch " Incremental search (shows as you type)
:set hlsearch " Highlight matches
:nohlsearch " Clear highlight (or :noh)
:set ignorecase " Case insensitive
:set smartcase " Smart case (sensitive if uppercase)
Exercises to Complete
-
[ ] Search for a word, navigate with
nandN -
[ ] Use
*on a variable name to find all uses -
[ ] Delete to a pattern with
d/ -
[ ] Try
cgnfor interactive find/replace -
[ ] Practice regex patterns for config navigation
Next Session
Session 05: Marks & Jump Lists - Never lose your place again.
Session Log
| Timestamp | Notes |
|---|---|
Start |
<Record when you started> |
End |
<Record when you finished> |
Discoveries |
<What surprised you?> |