Skip to content

Commit

Permalink
basics: separate search-replace into its own section
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Nov 24, 2023
1 parent a3556bf commit 0e304ec
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,138 +3,27 @@
Search and replace within the current line or buffer:

- `g m A` will match text under curor allowing in-place editing with visual-multi plugin
- `:%substitue` vim-style search and replace (I find this fiddley and not reliable, although could be user errror)
- `:%substitue` vim-style search and replace

Search and replace across a project:

- `SPC s` AstroNvim search and replace commands
- `SPC s` AstroNvim search and replace commands using Spectre (community plugin)
- Clojure LSP for symbols, etc.


??? HINT "Multiple cursors for multiple substitutions"
++"g"++ ++"m"++ ++"A"++ with the cursor on a word will start [multiple cursors](multiple-cursors.md) with a cursor on each occurance. Vim-editing tools can be used to replace the text at all cursors simultaneously


## Vim Sustitute
## Buffer wide

`:substitute` or `:s` vim command will highlight the matches for a text pattern and substitute for a new pattern

??? INFO "Neovim :help :substitute"
```vim
:help :substitute
```

Subsitute the first matching patterns in the current line

```vim
:s/current-pattern/new-pattern/
```

> If the new-pattern text is ommitted, then substitute deletes the current-pattern occurances, e.g `:s/current-pattern//`

Subsitute all the matching patterns in the current line, `g` representing all occurances in a line

```vim
:s/current-pattern/new-pattern/g
```

Use `%` to specify the current buffer as the scope to change all matches
Replace all occurances of the current-pattern with the new pattern within the buffer.

```vim
:%s/current-pattern/new-pattern/g
```

An inclusive line range can be specified to narrow the search

```vim
:4,24s/current-pattern/new-pattern/g
```

`.` can be used to represent the current line of buffer

`$` to represent the last line of the current buffer

```vim
:.,$s/current-pattern/new-pattern/g
```

Match the whole word


```vim
:.,$s/\<current-pattern\>/new-pattern/g
```

### Substitute history

`:s` and the ++arrow-up++ / ++arrow-down++ will navigate through the substitution history for the current session (from when Neovim was last opened if session was not restored)


### Confirm replacement

++"c"++ option at the end prompt for confirmation to replace each occurance

```vim
:%s/current-pattern/new-pattern/gc
```

++"y"++ confirms the repacement

++"l"++ confirms the repacement and quits

++"n"++ skips the current occurance and goes to the next one

++"y"++ or ++esc++ to quit substitution


### Regular expression

regular expressions can be used as a search pattern.

To replace all lines starting with ‘foo’ with ‘NeoVim Rocks’:

```vim
:%s/^foo.*/NeoVim rocks/gc
```

Replace all instances of ‘apple’, ‘orange’, and ‘mango’ with ‘fruit’:

```vim
:%s/apple\|orange\|mango/fruit/g
```

Remove trailing blank space at the end of each line:

```vim
:%s/\s\+$//e
```


## Matching case

`i` option disables the default case sensitive search

```vim
:%s/current-pattern/new-pattern/gi
```

### Visual Select

Use a visual select to search and replace, with confirmation

Note: `'<,'>` is automatically included when in visual mode and `:` is pressed to start a command

```vim
:'<,'>s/search-text/replace-text/g
```

A potentially more effecitve approach:

- visually select the text
- `*` to select all matching occurances
- `:%s//replace-text/g`

> Further examples of [`:substitue` neovim command](substitue.md)

## Project wide
Expand Down
120 changes: 120 additions & 0 deletions docs/neovim-basics/search-replace/substitute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Substitute command

`:substitute` or `:s` command highlights the matches for a text pattern and substitute for a new pattern

??? INFO "Neovim :help :substitute"
```vim
:help :substitute
```

Subsitute the first matching patterns in the current line

```vim
:s/current-pattern/new-pattern/
```

> If the new-pattern text is ommitted, then substitute deletes the current-pattern occurances, e.g `:s/current-pattern//`

Subsitute all the matching patterns in the current line, `g` representing all occurances in a line

```vim
:s/current-pattern/new-pattern/g
```

Use `%` to specify the current buffer as the scope to change all matches

```vim
:%s/current-pattern/new-pattern/g
```

An inclusive line range can be specified to narrow the search

```vim
:4,24s/current-pattern/new-pattern/g
```

`.` can be used to represent the current line of buffer

`$` to represent the last line of the current buffer

```vim
:.,$s/current-pattern/new-pattern/g
```

Match the whole word


```vim
:.,$s/\<current-pattern\>/new-pattern/g
```

### Substitute history

`:s` and the ++arrow-up++ / ++arrow-down++ will navigate through the substitution history for the current session (from when Neovim was last opened if session was not restored)


### Confirm replacement

++"c"++ option at the end prompt for confirmation to replace each occurance

```vim
:%s/current-pattern/new-pattern/gc
```

++"y"++ confirms the repacement

++"l"++ confirms the repacement and quits

++"n"++ skips the current occurance and goes to the next one

++"y"++ or ++esc++ to quit substitution


### Regular expression

regular expressions can be used as a search pattern.

To replace all lines starting with ‘foo’ with ‘NeoVim Rocks’:

```vim
:%s/^foo.*/NeoVim rocks/gc
```

Replace all instances of ‘apple’, ‘orange’, and ‘mango’ with ‘fruit’:

```vim
:%s/apple\|orange\|mango/fruit/g
```

Remove trailing blank space at the end of each line:

```vim
:%s/\s\+$//e
```


## Matching case

`i` option disables the default case sensitive search

```vim
:%s/current-pattern/new-pattern/gi
```

### Visual Select

Use a visual select to search and replace, with confirmation

Note: `'<,'>` is automatically included when in visual mode and `:` is pressed to start a command

```vim
:'<,'>s/search-text/replace-text/g
```

A potentially more effecitve approach:

- visually select the text
- `*` to select all matching occurances
- `:%s//replace-text/g`

0 comments on commit 0e304ec

Please sign in to comment.