• Stars
    star
    213
  • Rank 184,550 (Top 4 %)
  • Language
    Lua
  • Created over 1 year ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

🧐 search-replace.nvim

Screenshot

Supercharge the native Neovim search and replace experience!

✨ Features

  • Quick opening of :%s///gcI
  • Quick opening of :%s/<special selection>// where <special selection> refers to a predefined selection under the cursor. Please see: What are Special Selections?
  • A UI to preview the current special selections under the cursor
  • Quick opening of :%s/<visual selection>//gcI where <visual selection> 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

πŸŽ₯ 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 <leader>ro but could just as easily be mapped to <C-r> for faster access.

Search and Replace

Search and Replace

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

Search and Replace with Special Keys

Search and Replace with Special Keys

<leader>rw           # - open which-key 'replace' sub-menu, then use '[w]ord' under
                     #   cursor as search term
example_replacement  # - term to replace with
yyyynnyy<esc>        # - since 'c' flag is supplied to search/replace, confirm which
                     #   matches to search and replace
uuuuuu               # - undo changes
<leader>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

                     # - move to end of expression to replace
<leader>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<esc>            # - since 'c' flag is supplied to search/replace, confirm which
                     #   matches to search and replace
uuuuuu               # - undo changes
<leader>h            # - finish by disabling search term highlighting

Search and Replace Visual Charwise as Search String

Search and Replace Visual Charwise as Search String

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

Search and Replace Across Visual (Blockwise/Linewise) Selection

Search and Replace Across Visual Selection

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

⚑ What are Special Selections?

With the following example text:

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 *)

# Selection:
lv|im.builtin.which_key.mappings["r"]["w"]
^^^^^
# Value:
lvim
# Selection:
lvim.bui|ltin.which_key.mappings["r"]["w"]
     ^^^^^^^^
# Value:
builtin
# Selection:
lvim.builtin.whi|ch_key.mappings["r"]["w"]
             ^^^^^^^^^^
# Value:
which_key
# Selection:
lvim.builtin.which_key.mapp|ings["r"]["w"]
                       ^^^^^^^^^
# Value:
mappings

CWORD

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

# Selection:
lv|im.builtin.which_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key.mappings["r"]["w"]
# 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.

# Selection:
lvim.bui|ltin.which_key.mappings["r"]["w"]
^^^^^^^^^^^^^
# Value:
lvim.builtin
# Selection:
lvim.builtin.wh|ich_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key
# 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)

# Selection:
lvim.bui|ltin.which_key.mappings["r"]["w"]
^^^^^^^^^^^^^
# Value:
lvim.builtin
# Selection:
lvim.builtin.wh|ich_key.mappings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key
# Selection:
lvim.builtin.which_key.map|pings["r"]["w"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Value:
lvim.builtin.which_key.mappings

πŸ”¬ 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

πŸš€ Installation

Lazy.nvim

{
  "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

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,
})

πŸ› οΈ Usage

Standard Neovim

local opts = {}
vim.api.nvim_set_keymap("v", "<C-r>", "<CMD>SearchReplaceSingleBufferVisualSelection<CR>", opts)
vim.api.nvim_set_keymap("v", "<C-s>", "<CMD>SearchReplaceWithinVisualSelection<CR>", opts)
vim.api.nvim_set_keymap("v", "<C-b>", "<CMD>SearchReplaceWithinVisualSelectionCWord<CR>", opts)

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

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

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

Lunarvim / Which-Key

keymap = lvim.builtin.which_key.mappings

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

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

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

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

lvim.keys.visual_block_mode["<C-r>"] = [[<CMD>SearchReplaceSingleBufferVisualSelection<CR>]]
lvim.keys.visual_block_mode["<C-s>"] = [[<CMD>SearchReplaceWithinVisualSelection<CR>]]
lvim.keys.visual_block_mode["<C-b>"] = [[<CMD>SearchReplaceWithinVisualSelectionCWord<CR>]]

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

More Repositories

1

tailwindcss-colorizer-cmp.nvim

🌈 A Neovim plugin to add vscode-style TailwindCSS completion to nvim-cmp
Lua
322
star
2

activate.nvim

πŸš€ Activate is a plugin installation system for Neovim
Lua
111
star
3

surround-ui.nvim

πŸ€— A Neovim plugin which acts as a helper or training aid for kylechui/nvim-surround
Lua
76
star
4

action-hints.nvim

⚑ A Neovim plugin that displays available actions like 'Go to Definition' and 'Go to Reference(s)' for the highlighted word, presented in the statusline or inline as virtual text.
Lua
76
star
5

palette.nvim

🎨 Palette - A beautiful, versatile, systematic, Neovim theme system
Lua
64
star
6

bufferline-cycle-windowless.nvim

πŸͺŸ A Neovim/Bufferline extension to cycle through windowless buffers to give a more traditional tab based experience
Lua
39
star
7

hoversplit.nvim

🚁 Automatically updated documentation and information about code symbols in a split window
Lua
38
star
8

tabtree.nvim

🌲 A Neovim plugin for jumping between significant code elements, such as brackets, quotes, etc.
Lua
38
star
9

f-string-toggle.nvim

🧢 A Neovim plugin to toggle python f-strings
Lua
30
star
10

cloudwatch-loki-shipper

Python
20
star
11

node-type.nvim

🌐 A Neovim plugin to show the currently selected node type from lsp and treesitter information
Lua
20
star
12

dotfiles

Lua
11
star
13

twin-sh

πŸͺ„ This Week in NeoVIM - Contribution Wizard
Shell
9
star
14

nightshift.nvim

✨ A dark low-contrast Neovim theme
Lua
8
star
15

erb-hiera

Document generation from a scope, ERB template(s) and Hiera data
Ruby
7
star
16

tldr-lang.nvim

πŸƒ A Neovim plugin to give tldr-style docs for various language features
Lua
7
star
17

sensu_handler_graphite_event

Ruby
4
star
18

neoscroll-motions.nvim

Paragraph support for neoscroll
Lua
4
star
19

gps-tracker-tk103

JavaScript
3
star
20

workstation-elk_stack

3
star
21

gke-cluster-nodepool-scaler

Python
3
star
22

roobert

Python
2
star
23

roobert.github.io

SCSS
2
star
24

variable-updater

Python
2
star
25

arduino-speedometer

C++
1
star
26

matasano_crypto_challenge

Ruby
1
star
27

github-action-helpers

Python
1
star
28

mcollective_agent-network_interfaces

Ruby
1
star
29

mongodb_atlas_cluster_pauser

Python
1
star
30

packer-openstack-puppet

Shell
1
star
31

uchiwa-elasticsearch-logstash-proxy

Ruby
1
star
32

redis-what-the-role

Shell
1
star
33

multitude-ui

Vue
1
star
34

firestore-model

Python
1
star
35

gcp-service-account-key-cleaner

Python
1
star
36

pluto-pre-commit-hook

Shell
1
star
37

aws-lambda-sqs-elasticsearch

Python
1
star
38

prometheus-gke-letsencrypt-certs

Ruby
1
star
39

dms

Go
1
star
40

sensu-run

Shell
1
star
41

homebrew-neovim-0-5-0

Ruby
1
star
42

kubernetes-development-environment

A profile based development system for working in a kubernetes pod with mounted data bucket
Shell
1
star
43

cat-facts-api

Python
1
star
44

route53-dynamic_dns_update

Ruby
1
star
45

lunarvim.org

🌐 Website for LunarVim
JavaScript
1
star
46

multitude

Application deployment pipeline dashboard.
Python
1
star
47

prometheus-gcp-ssl-certs

Ruby
1
star
48

grafana-influx-mqtt-tasmota

Python
1
star
49

iac-application-template-pre-commit-hook

Shell
1
star
50

robsvan

HTML
1
star