diff options
author | Josh Rahm <rahm@google.com> | 2025-04-04 17:36:05 +0000 |
---|---|---|
committer | Josh Rahm <rahm@google.com> | 2025-04-04 17:36:05 +0000 |
commit | 9e99546a2dc05803ded7a664cb319244d91a010b (patch) | |
tree | 21d4e15b6c020bdb957c2214c75575848a845377 | |
parent | 77a792caa943495a31bef6eeb85fb72bd0af1f88 (diff) | |
download | fieldmarshal.vim-9e99546a2dc05803ded7a664cb319244d91a010b.tar.gz fieldmarshal.vim-9e99546a2dc05803ded7a664cb319244d91a010b.tar.bz2 fieldmarshal.vim-9e99546a2dc05803ded7a664cb319244d91a010b.zip |
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.
-rw-r--r-- | plugin/substitute.vim | 50 |
1 files 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 <silent> gs :call <SID>v_do_subst_enter()<cr> noremap <silent> gS :set operatorfunc=<SID>do_subst_no_enter<cr>g@ vnoremap <silent> gS :call <SID>v_do_subst_no_enter()<cr> +" Hotkey for replacing the most recently searched matches. +noremap <C-s> <esc>:<C-u>%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('<cword>') + 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("<cword>") + 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("<cword>") + else + let s:new_word = expand("<cword>") + endif +endfunction + +augroup FieldMarshalSubstitute + au! + autocmd InsertEnter * call s:save_old_word() + autocmd InsertLeave * call s:save_new_word() +augroup END + +noremap <expr> c OverrideChange() . 'c' +noremap <expr> s OverrideChange() . 's' +noremap <expr> 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 |