# Neovim Searching and Replacing **Search and replace** are fundamental for editing large files and maintaining consistency. Neovim uses standard regex patterns. ## Basic search `/pattern` — search forward, `?pattern` — search backward. Type pattern, press `Enter` to jump to first match. `n` — next match in same direction, `N` — previous match (reverse direction). `*` — search for word under cursor forward, `#` — search backward. Clear last search highlighting: `:nohlsearch` or `:set hlsearch` / `:set nohlsearch` to toggle. ```vim /function " search for "function" n " next match N " previous match * " search word at cursor ``` ## Case-insensitive search `:set ignorecase` — makes search case-insensitive. `:set smartcase` — ignores case unless you use uppercase in pattern. ```vim :set ignorecase /CONST " finds const, Const, CONST ``` Inline toggle: append `\c` to pattern for case-insensitive, `\C` for case-sensitive. ```vim /Pattern\c " search case-insensitive ``` ## Find and replace `:s/old/new` — substitute in current line, `:s/old/new/g` — all occurrences in line. `:%s/old/new` — first occurrence per line in file, `:%s/old/new/g` — all occurrences in file. `:%s/old/new/gc` — replace with confirm (shows each match, prompt `y`/`n`). `:%s/old/new/i` — case-insensitive. ```vim :s/const/let/ " first per line :s/const/let/g " all in line :%s/old/new/g " all in file :%s/foo/bar/gc " confirm each ``` ## Regex patterns Patterns support regex. `.` matches any char, `*` is 0 or more, `+` is 1 or more, `?` is 0 or 1. `^` — line start, `$` — line end. `\<` — word boundary start, `\>` — word boundary end. Groups: `\(pattern\)` captures, `\1` refers to first group. ```vim :%s/\/new/g " whole words only :%s/^/> /g " add "> " to line start :%s/\(.\)\(.\)/\2\1/g " swap adjacent chars ``` ## Substitution with addresses `:10,20s/old/new/g` — replace in lines 10–20. `:.,+5s/old/new/` — replace in current and next 5 lines. `:'<,'>s/old/new/g` — replace in visual selection (select text, type command). ## Search within selection Visual select, then `:` auto-fills `:'<,'>`. Type the substitute command. ```vim v5j " select 5 lines :s/old/new/g " replace in selection ``` Quick workflow: search with `/`, view context, `:set hlsearch` highlights all, then `:%s/old/new/gc` to replace with confirmation.