From 9e99546a2dc05803ded7a664cb319244d91a010b Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 4 Apr 2025 17:36:05 +0000 Subject: Change how the substitute works. The strategy is now, what was the word before the text was changed, and what is it after, and have that be the search/replace. --- plugin/substitute.vim | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/plugin/substitute.vim b/plugin/substitute.vim index 47de404..f06afb9 100644 --- a/plugin/substitute.vim +++ b/plugin/substitute.vim @@ -20,6 +20,52 @@ vnoremap gs :call v_do_subst_enter() noremap gS :set operatorfunc=do_subst_no_enterg@ vnoremap gS :call v_do_subst_no_enter() +" Hotkey for replacing the most recently searched matches. +noremap :%s// + +let s:entered_with_change = v:false +let s:entered_with_append = v:false + +let s:old_word='' +let s:new_word='' + +let s:override_word = '' +function! OverrideChange() + let s:entered_with_change = v:true + let s:override_word = expand('') + return '' +endfunction + +function! s:save_register_on_change() +endfunction + +function! s:save_old_word() abort + if s:entered_with_change + let s:old_word = s:override_word + else + let s:old_word = expand("") + endif +endfunction + +function! s:save_new_word() abort + if s:entered_with_change + let s:entered_with_change = v:false + let s:new_word = expand("") + else + let s:new_word = expand("") + endif +endfunction + +augroup FieldMarshalSubstitute + au! + autocmd InsertEnter * call s:save_old_word() + autocmd InsertLeave * call s:save_new_word() +augroup END + +noremap c OverrideChange() . 'c' +noremap s OverrideChange() . 's' +noremap a OverrideChange() . 'a' + function! s:do_subst_enter(...) abort call s:do_subst_priv("'[", "']", v:true) endfunction @@ -50,8 +96,8 @@ function! s:do_subst_priv(m0, m1, do_enter) abort \ lnum0, \ lnum1, \ a:do_enter ? "\\<" : "", - \ escape(@", '/\'), + \ escape(s:old_word, '/\'), \ a:do_enter ? "\\>" : "", - \ escape(@., '/\'), + \ escape(s:new_word, '/\'), \ a:do_enter ? "\n" : "")) endfunction -- cgit