From f7bf242a9507fe948efaf544915462c47aa45892 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Mon, 27 May 2024 14:55:42 -0500 Subject: [PATCH] feat: substitute downward then jump to the top for the rest 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. --- plugin/scalpel.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugin/scalpel.lua b/plugin/scalpel.lua index bc6c7c3..ab9c2bf 100644 --- a/plugin/scalpel.lua +++ b/plugin/scalpel.lua @@ -4,8 +4,16 @@ local function substitute_current_word() local word = vim.fn.expand('') - 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( + '', + true, + false, + true + ) + + vim.api.nvim_feedkeys(pattern .. cursor_move, 'n', true) end vim.keymap.set('n', 'e', substitute_current_word, { desc = 'Substite current word in file' })