Skip to content

Commit

Permalink
Merge pull request #3 from wassimk/wm/visual-line-mode
Browse files Browse the repository at this point in the history
feat: support visual line mode for substituting within a whole line range
  • Loading branch information
wassimk authored Jun 5, 2024
2 parents 9201410 + df01798 commit ff76774
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<leader>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.
34 changes: 25 additions & 9 deletions doc/scalpel.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<leader>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 <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.
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*
Expand Down
7 changes: 4 additions & 3 deletions lua/scalpel/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ function M.substitute()
local word = get_substitution_word()

if vim.fn.mode() == 'V' then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', 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('<Left><Left><Left>', 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
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('<Left><Left><Left>', true, false, true)
vim.api.nvim_feedkeys(pattern .. cursor_move, 'n', true)
end
Expand Down

0 comments on commit ff76774

Please sign in to comment.