From d7d202d90e439a143282694be95a2e16abec9198 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Wed, 5 Jun 2024 15:03:18 -0500 Subject: [PATCH 1/3] feat: implement support for visual line mode --- README.md | 29 +++++++++++++++++++++++------ lua/scalpel/init.lua | 5 +++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 27280ab..254eb9c 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,33 @@ The following example shows the installation process using [lazy.nvim](https://g } ``` +> [!NOTE] +> You can set the keymap to anything you wish. + ## Usage -Using **scalpel.nvim** is simple: +#### Normal Mode + +1. Move the cursor over the word to replace +2. Trigger the substitution with your keymap +3. Begin typing the desired substitution and hit return + +#### Visual Mode + +1. Use visual mode (`v`) to select the word(s) to replace within a *single line* +2. Trigger the substitution with your keymap +3. Begin typing the desired substitution and hit return -1. Move the cursor over the word you wish to replace. -2. Trigger the substitution with the `e` keymap. -3. Begin typing your desired substitution and hit return. Note that the original word is being captured, so if you would like to incorporate it into your replacement, use `\1`. +#### Visual Line Mode -This plugin also supports visual mode selection for substitutions within a single line. +1. Highlight word(s) to substitute with `*` or `/` +2. Use visual line mode (`V`) to highlight the lines with the word(s) to substitute +3. Trigger the substitution with your keymap +4. Begin typing the desired substitution and hit return +> [!TIP] +> The word(s) being replaced during substitution are available in the replacement text using `&`. +> ## Acknowledgments -This project was inspired by [Scalpel](https://github.com/wincent/scalpel), a Vimscript plugin I've used for many years. **scalpel.nvim** is my version reimagined and implemented in Lua for fun. +This project was inspired by [Scalpel](https://github.com/wincent/scalpel), a Vimscript plugin I've used for many years. **scalpel.nvim** is my version, which was reimagined and implemented in Lua for fun. diff --git a/lua/scalpel/init.lua b/lua/scalpel/init.lua index 6ef335c..16d8784 100644 --- a/lua/scalpel/init.lua +++ b/lua/scalpel/init.lua @@ -56,8 +56,9 @@ function M.substitute() local word = get_substitution_word() if vim.fn.mode() == 'V' then - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', false, true, true), 'nx', false) - vim.notify('Visual line mode not supported for substitution selection', vim.log.levels.WARN) + local pattern = ':s///gc' + local cursor_move = vim.api.nvim_replace_termcodes('', true, false, true) + vim.api.nvim_feedkeys(pattern .. cursor_move, 'n', true) elseif word == nil then vim.notify('Cannot substitute this selection', vim.log.levels.INFO) elseif is_blank(word) then From 62ad3ea1a93d0d28b4ed5cd198b6177e7e15c4f2 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Wed, 5 Jun 2024 15:05:31 -0500 Subject: [PATCH 2/3] feat!: stop capturing patterns with () for \1 and use & instead The & captures the pattern already and can be used in the replacement text. --- lua/scalpel/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/scalpel/init.lua b/lua/scalpel/init.lua index 16d8784..0f3244d 100644 --- a/lua/scalpel/init.lua +++ b/lua/scalpel/init.lua @@ -64,7 +64,7 @@ function M.substitute() elseif is_blank(word) then vim.notify('Selection is blank, cannot substitute', vim.log.levels.INFO) else - local pattern = ':%s/\\v(' .. word .. ')//gc' + local pattern = ':%s/\\v' .. word .. '//gc' local cursor_move = vim.api.nvim_replace_termcodes('', true, false, true) vim.api.nvim_feedkeys(pattern .. cursor_move, 'n', true) end From df017984eb69002f6949082987a120cefd3851cb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 5 Jun 2024 20:06:59 +0000 Subject: [PATCH 3/3] build(docs): auto generate vim documentation --- doc/scalpel.nvim.txt | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/doc/scalpel.nvim.txt b/doc/scalpel.nvim.txt index 978fdda..94cdbb3 100644 --- a/doc/scalpel.nvim.txt +++ b/doc/scalpel.nvim.txt @@ -46,23 +46,39 @@ The following example shows the installation process using lazy.nvim < + [!NOTE] You can set the keymap to anything you wish. + USAGE *scalpel.nvim-scalpel.nvim-usage* -Using **scalpel.nvim** is simple: -1. Move the cursor over the word you wish to replace. -2. Trigger the substitution with the `e` keymap. -3. Begin typing your desired substitution and hit return. Note that the original word is being captured, so if you would like to incorporate it into your replacement, use `\1`. +NORMAL MODE + +1. Move the cursor over the word to replace +2. Trigger the substitution with your keymap +3. Begin typing the desired substitution and hit return + + +VISUAL MODE + +1. Use visual mode (`v`) to select the word(s) to replace within a _single line_ +2. Trigger the substitution with your keymap +3. Begin typing the desired substitution and hit return + -This plugin also supports visual mode selection for substitutions within a -single line. +VISUAL LINE MODE +1. Highlight word(s) to substitute with `*` or `/` +2. Use visual line mode (`V`) to highlight the lines with the word(s) to substitute +3. Trigger the substitution with your keymap +4. Begin typing the desired substitution and hit return -ACKNOWLEDGMENTS *scalpel.nvim-scalpel.nvim-acknowledgments* + [!TIP] The word(s) being replaced during substitution are available in the + replacement text using `&`. + ACKNOWLEDGMENTS *scalpel.nvim-scalpel.nvim-acknowledgments* This project was inspired by Scalpel , a -Vimscript plugin I’ve used for many years. **scalpel.nvim** is my version -reimagined and implemented in Lua for fun. +Vimscript plugin I’ve used for many years. **scalpel.nvim** is my version, +which was reimagined and implemented in Lua for fun. ============================================================================== 2. Links *scalpel.nvim-links*