Skip to content

Commit

Permalink
feat: substitute downward then jump to the top for the rest
Browse files Browse the repository at this point in the history
The substitute command with % as the pattern is really 1,$. Which is
first line to the last line. So when the substitution starts, if there
are matches above the cursor line the cursor will jump up to the first
match and start the substituting downward.

This feels jarring. So let's try to address that by starting at the
current position down. Then repeating the last substitute with a new
range of 1st line to the line above the cursor position.

This is a lot of code for this nicety. Time will tell if it's worth it.
  • Loading branch information
wassimk committed May 27, 2024
1 parent 87442f3 commit f7bf242
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions plugin/scalpel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

local function substitute_current_word()
local word = vim.fn.expand('<cword>')
local pattern = ':%s/\\v(' .. word .. ')'
vim.api.nvim_feedkeys(pattern, 'n', true)
local pattern = ':,$s/\\v(' .. word .. ")//gc|:undojoin|1,''-&&"

local cursor_move = vim.api.nvim_replace_termcodes(
'<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>',
true,
false,
true
)

vim.api.nvim_feedkeys(pattern .. cursor_move, 'n', true)
end

vim.keymap.set('n', '<leader>e', substitute_current_word, { desc = 'Substite current word in file' })

0 comments on commit f7bf242

Please sign in to comment.