Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/roobert/search-replace.nvim

:monocle_face: A Neovim search and replace plugin that builds on the native search and replace experience.
https://github.com/roobert/search-replace.nvim

Last synced: 14 days ago
JSON representation

:monocle_face: A Neovim search and replace plugin that builds on the native search and replace experience.

Awesome Lists containing this project

README

        

# :monocle_face: search-replace.nvim

![Screenshot](https://user-images.githubusercontent.com/226654/210175428-82e56d0c-0db2-418a-b74c-1ab5a03b3530.gif)

Supercharge the native Neovim search and replace experience!

## :sparkles: Features

* Quick opening of `:%s///gcI`
* Quick opening of `:%s///` where `` refers to a
predefined selection under the cursor. Please see: [What are Special Selections?](https://github.com/roobert/search-replace.nvim#zap-what-are-special-selections)
* A UI to preview the current special selections under the cursor
* Quick opening of `:%s///gcI` where `` is a
visual-charwise selection
* Configuration of the default flags passed to search and replace, e.g: `gcI` when
searching across a buffer with `:%s///gcI`
* Support for search and replace over multiple buffers
* A command to search and replace over a visual-block/visual-linewise/visual-charwise
selection
* Example key mappings

## :movie_camera: Demos

Most of the following examples use `which-key` and `leader` key bindings but the available plugin commands can
be bound to any keymapping, for example replacement opening is bound to `ro` but could
just as easily be mapped to `` for faster access.

### Search and Replace

![Search and Replace](https://user-images.githubusercontent.com/226654/210173887-45b157ef-eb4b-4a6e-9442-29f7010d3084.gif)

``` lua
ro # - open which-key 'replace' sub-menu, then 'open' a search
lvim # - term to search
example_replacement # - term to replace with
yyyynnyy # - since 'c' flag is supplied to search/replace, confirm which
# matches to search and replace
uuuuuu # - undo changes
h # - finish by disabling search term highlighting
```

### Search and Replace with Special Keys

![Search and Replace with Special Keys](https://user-images.githubusercontent.com/226654/210173893-11f0189e-2971-4f23-93da-a915e8ede0dd.gif)

``` lua
rw # - open which-key 'replace' sub-menu, then use '[w]ord' under
# cursor as search term
example_replacement # - term to replace with
yyyynnyy # - since 'c' flag is supplied to search/replace, confirm which
# matches to search and replace
uuuuuu # - undo changes
h # - finish by disabling search term highlighting
```

### Search and Replace with Special Keys and Search Hinting UI

![Search and Replace with Special Keys and Search Hinting UI
](https://user-images.githubusercontent.com/226654/210174276-5dd39c57-2ce7-4de0-bc2a-274cd5b4a677.gif)

``` lua
# - move to end of expression to replace
rs # - open which-key 'replace' sub-menu, then 'selections' UI
e # - specify '[e]xpr' special key to use as search term
example.replacement # - term to replace with
yyyy # - since 'c' flag is supplied to search/replace, confirm which
# matches to search and replace
uuuuuu # - undo changes
h # - finish by disabling search term highlighting
```

### Search and Replace Visual Charwise as Search String

![Search and Replace Visual Charwise as Search String
](https://user-images.githubusercontent.com/226654/210175020-aaa1a6fa-7fb8-4d87-ade6-20fa391e1a57.gif)

``` lua
v # - highlight the string to be replaced with visual-charwise mode
# - key binding to replace with selection
example_replacement # - term to replace with
yyyynnyy # - since 'c' flag is supplied to search/replace, confirm which
# matches to search and replace
uuuuuu # - undo changes
h # - finish by disabling search term highlighting
```

### Search and Replace Across Visual (Blockwise/Linewise) Selection

![Search and Replace Across Visual Selection](https://user-images.githubusercontent.com/226654/210175210-0da9063b-e933-451d-bc90-2405ad9e03f0.gif)

``` lua
V # - highlight multiple lines with a visual-linewise selection
# - key binding to open search across block
# - move cursor to highlight search term
example_replacement # - term to replace with
yynn # - since 'c' flag is supplied to search/replace, confirm which
# matches to search and replace
uu # - undo changes
h # - finish by disabling search term highlighting
```

## :zap: What are Special Selections?

With the following example text:

``` lua
lvim.builtin.which_key.mappings["r"]["w"]
```

And the cursor position shown as `|`

The following examples are `true`.

### CWord

`CWord` is replaced with the `word` under the cursor (like `*`)

``` lua
# Selection:
lv|im.builtin.which_key.mappings["r"]["w"]
^^^^^
# Value:
lvim
```

``` lua
# Selection:
lvim.bui|ltin.which_key.mappings["r"]["w"]
^^^^^^^^
# Value:
builtin
```

``` lua
# Selection:
lvim.builtin.whi|ch_key.mappings["r"]["w"]
^^^^^^^^^^
# Value:
which_key
```

``` lua
# Selection:
lvim.builtin.which_key.mapp|ings["r"]["w"]
^^^^^^^^^
# Value:
mappings
```

### CWORD

`CWORD` is replaced with the `WORD` under the cursor (like greedy `word`)

``` lua
# Selection:
lv|im.builtin.which_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key.mappings["r"]["w"]
```

``` lua
# Selection:
lvim.builtin.whi|ch_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key.mappings["r"]["w"]
```

### CExpr

`CExpr` is replaced with the `word` under the cursor, including more to form a C expression.

``` lua
# Selection:
lvim.bui|ltin.which_key.mappings["r"]["w"]
^^^^^^^^^^^^^
# Value:
lvim.builtin
```

``` lua
# Selection:
lvim.builtin.wh|ich_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key
```

``` lua
# Selection:
lvim.builtin.which_key.map|pings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key.mappings
```

### CFile

`CFile` is replaced with the path name under the cursor (like what `gf` uses)

``` lua
# Selection:
lvim.bui|ltin.which_key.mappings["r"]["w"]
^^^^^^^^^^^^^
# Value:
lvim.builtin
```

``` lua
# Selection:
lvim.builtin.wh|ich_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key
```

``` lua
# Selection:
lvim.builtin.which_key.map|pings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key.mappings
```

## :microscope: Available Commands

### Single Buffer - Open Search

* `SearchReplaceSingleBufferOpen`
* `SearchReplaceMultiBufferOpen`

### Single Buffer - Open Search with Special Key as Search Term

* `SearchReplaceSingleBufferCWord`
* `SearchReplaceSingleBufferCWORD`
* `SearchReplaceSingleBufferCExpr`
* `SearchReplaceSingleBufferCFile`

### Multi Buffer - Open Search with Special Key as Search Term

* `SearchReplaceMultiBufferCWord`
* `SearchReplaceMultiBufferCWORD`
* `SearchReplaceMultiBufferCExpr`
* `SearchReplaceMultiBufferCFile`

### Single/Multi Buffer UI - Show Special Key Values and Shortcuts to Open Search Replace

* `SearchReplaceSingleBufferSelections`
* `SearchReplaceMultiBufferSelections`

### Single Buffer - Visual Charwise as Search Term

* `SearchReplaceSingleBufferWithinBlock`

### Search over Visual (Blockwise/Linewise) Selection

* `SearchReplaceVisualSelection`
* `SearchReplaceVisualSelectionCWord`
* `SearchReplaceVisualSelectionCWORD`
* `SearchReplaceVisualSelectionCExpr`
* `SearchReplaceVisualSelectionCFile`

## :rocket: Installation

### Lazy.nvim

``` lua
{
"roobert/search-replace.nvim",
config = function()
require("search-replace").setup({
-- optionally override defaults
default_replace_single_buffer_options = "gcI",
default_replace_multi_buffer_options = "egcI",
})
end,
}
```

### Packer.nvim

``` lua
use({
"roobert/search-replace.nvim",
config = function()
require("search-replace").setup({
-- optionally override defaults
default_replace_single_buffer_options = "gcI",
default_replace_multi_buffer_options = "egcI",
})
end,
})
```

## :hammer_and_wrench: Usage

### Standard Neovim

``` lua
local opts = {}
vim.api.nvim_set_keymap("v", "", "SearchReplaceSingleBufferVisualSelection", opts)
vim.api.nvim_set_keymap("v", "", "SearchReplaceWithinVisualSelection", opts)
vim.api.nvim_set_keymap("v", "", "SearchReplaceWithinVisualSelectionCWord", opts)

vim.api.nvim_set_keymap("n", "rs", "SearchReplaceSingleBufferSelections", opts)
vim.api.nvim_set_keymap("n", "ro", "SearchReplaceSingleBufferOpen", opts)
vim.api.nvim_set_keymap("n", "rw", "SearchReplaceSingleBufferCWord", opts)
vim.api.nvim_set_keymap("n", "rW", "SearchReplaceSingleBufferCWORD", opts)
vim.api.nvim_set_keymap("n", "re", "SearchReplaceSingleBufferCExpr", opts)
vim.api.nvim_set_keymap("n", "rf", "SearchReplaceSingleBufferCFile", opts)

vim.api.nvim_set_keymap("n", "rbs", "SearchReplaceMultiBufferSelections", opts)
vim.api.nvim_set_keymap("n", "rbo", "SearchReplaceMultiBufferOpen", opts)
vim.api.nvim_set_keymap("n", "rbw", "SearchReplaceMultiBufferCWord", opts)
vim.api.nvim_set_keymap("n", "rbW", "SearchReplaceMultiBufferCWORD", opts)
vim.api.nvim_set_keymap("n", "rbe", "SearchReplaceMultiBufferCExpr", opts)
vim.api.nvim_set_keymap("n", "rbf", "SearchReplaceMultiBufferCFile", opts)

-- show the effects of a search / replace in a live preview window
vim.o.inccommand = "split"
```

### Lunarvim / Which-Key

``` lua
keymap = lvim.builtin.which_key.mappings

keymap["r"] = { name = "SearchReplaceSingleBuffer" }

keymap["r"]["s"] =
{ "SearchReplaceSingleBufferSelections", "SearchReplaceSingleBuffer [s]elction list" }
keymap["r"]["o"] = { "SearchReplaceSingleBufferOpen", "[o]pen" }
keymap["r"]["w"] = { "SearchReplaceSingleBufferCWord", "[w]ord" }
keymap["r"]["W"] = { "SearchReplaceSingleBufferCWORD", "[W]ORD" }
keymap["r"]["e"] = { "SearchReplaceSingleBufferCExpr", "[e]xpr" }
keymap["r"]["f"] = { "SearchReplaceSingleBufferCFile", "[f]ile" }

keymap["r"]["b"] = { name = "SearchReplaceMultiBuffer" }

keymap["r"]["b"]["s"] =
{ "SearchReplaceMultiBufferSelections","SearchReplaceMultiBuffer [s]elction list" }
keymap["r"]["b"]["o"] = { "SearchReplaceMultiBufferOpen", "[o]pen" }
keymap["r"]["b"]["w"] = { "SearchReplaceMultiBufferCWord", "[w]ord" }
keymap["r"]["b"]["W"] = { "SearchReplaceMultiBufferCWORD", "[W]ORD" }
keymap["r"]["b"]["e"] = { "SearchReplaceMultiBufferCExpr", "[e]xpr" }
keymap["r"]["b"]["f"] = { "SearchReplaceMultiBufferCFile", "[f]ile" }

lvim.keys.visual_block_mode[""] = [[SearchReplaceSingleBufferVisualSelection]]
lvim.keys.visual_block_mode[""] = [[SearchReplaceWithinVisualSelection]]
lvim.keys.visual_block_mode[""] = [[SearchReplaceWithinVisualSelectionCWord]]

-- show the effects of a search / replace in a live preview window
vim.o.inccommand = "split"
```