global search replace option to show-subst-pause-undo needed

13 views
Skip to first unread message

Mosh hmi-tech.net

unread,
Nov 5, 2022, 3:13:35 PM11/5/22
to vim...@googlegroups.com
When I do complex substitutions,  I want to see inspect subst,  
undo the substitution, before moving to the next one match
(gnuemacs has something similar called "recursive-edit")

Eg. my file contains thousands of dates as seconds, and I want to reformat
the date (but seeing the context around the date)
:%s,\<\(1[45678]\d\d\d\d\d\d\d\d\)\>,\=system("date +' %F-%H:%M' -d @".submatch(1)." "),gc

 I need an option to show-susbt-pause-undo the substitution and continue to next.
p[subst and pause]/u [undo, and goto next] in

The options y/n/a/q/l/^E/^Y   
Help:
:h :s
[c] Confirm each substitution.  Vim highlights the matching string (with
|hl-IncSearch|).  You can type: *:s_c*
   'y'    to substitute this match
   'l'    to substitute this match and then quit ("last")
   'n'    to skip this match
   <Esc>   to quit substituting
   'a'    to substitute this and all remaining matches
   'q'    to quit substituting
   CTRL-E  to scroll the screen up
   CTRL-Y  to scroll the screen down          

thanks
Mosh

Bram Moolenaar

unread,
Nov 5, 2022, 6:58:08 PM11/5/22
to vim...@googlegroups.com, Mosh hmi-tech.net
Hmm, you should just not use :substitute for that. Use the right search
pattern, then record a change into a register (so you can repeat it).
You can also record two or more different changes, and use the one you
like with @a, @b, etc. And undo if it wasn't the right one and retry at
the same position. Then finally hit "n" to go to the next match.

There isn't really a good reason to expand the functionality of
:substitute to make it even more complex. It is really meant for bulk
changes, not complex stuff.

--
If they don't keep on exercising their lips, he thought, their brains
start working.
-- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

ben.k...@gmail.com

unread,
Nov 8, 2022, 10:22:10 AM11/8/22
to vim_dev
On Saturday, November 5, 2022 at 3:13:35 PM UTC-4 mo...@hmi-tech.net wrote:
When I do complex substitutions,  I want to see inspect subst,  
undo the substitution, before moving to the next one match
(gnuemacs has something similar called "recursive-edit")

Eg. my file contains thousands of dates as seconds, and I want to reformat
the date (but seeing the context around the date)
:%s,\<\(1[45678]\d\d\d\d\d\d\d\d\)\>,\=system("date +' %F-%H:%M' -d @".submatch(1)." "),gc

One way to do that would be:
1. /\v<1[45678]\d{8}>
2. qqcgn<C-r>=system("date +' %F-%H:%M' -d @<C-r>" ")<enter><esc>q
3. Use n/N to move between matches; u/C-r to undo/redo; and @q to perform the change.

(The macro is because, while cgn is actually .-repeatable, the use of the register inside <C-r>= isn't—the text returned by <C-r>= is considered the "change".)
Reply all
Reply to author
Forward
0 new messages