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/t for character search

  • Understand operators

  • Use / search as a motion

  • Know word-under-cursor 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
# This is a comment
actual_code_starts_here
/^[^#]<CR>   " First non-comment line
d?^#<CR>     " Delete back to comment

Summary: Search Commands

Command Effect

/pattern

Search forward

?pattern

Search backward

n

Next match (same direction)

N

Next match (opposite)

*

Word under cursor (forward, exact)

#

Word under cursor (backward, exact)

g*

Partial word forward

g#

Partial word backward

gn

Motion to next match (for operators)

gN

Motion to previous match

Search as Motion Reference

Command Effect

d/pattern

Delete to pattern

c/pattern

Change to pattern

y/pattern

Yank to pattern

v/pattern

Visual to pattern

cgn

Change next search match

dgn

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

  1. [ ] Search for a word, navigate with n and N

  2. [ ] Use * on a variable name to find all uses

  3. [ ] Delete to a pattern with d/

  4. [ ] Try cgn for interactive find/replace

  5. [ ] 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?>