2

I'm trying to use the global command :g with :substitute to find and replace some string and also with check.

So the command like:

g/\d/s/.*//gc

but the check behavior is not same as in :s

I can't quit or stop with ESC or q
Whatever I enter ESC or q message will still shows replace with (y/n/a/q/l/^E/^Y)? until the end of match result.

Is there any way to do the same behivor with check as :s in :g ?

1
  • Do you still have something open in your question? How can we help you further? Otherwise may be you could accept one of the solutions using the v button next to the arrow voting buttons. It allow the question to rest :-) Dec 19, 2023 at 4:50

2 Answers 2

2

The :g command enter in a loop that q, Esc doesn't break

For your case I would do:

:%s/.*\d.*//gc

Remark: Ctrl c break the loop of Vim and Neovim (for both nvim and nvim-qt) but not for gVim.

3
  • 1
    Is there any way to break in :g using :s with c?
    – M_Sea
    Dec 17, 2023 at 0:27
  • I don't think so :-/ Dec 17, 2023 at 6:58
  • Given the other answer, that question makes no sense.
    – romainl
    Dec 17, 2023 at 19:07
1

:help :g can definitely be aborted with <C-c>, as per the documentation:

Use "CTRL-C" to interrupt the command.

The /g, /c, etc. flags are features of :help :s. :g doesn't have that and there is no easy way I know of to somehow simulate it.

Furthermore, /c (for "confirm", not "check") only works for the current substitution. When you do:

:g/\d/s/.*//gc

you are actually doing as many substitutions as there are lines with digits in them. Because there is no delay or visible feedback when leaving one substitution and entering another, it all feels like a single substitution, but it really is not. This means that q effectively "quits" the current substitution, only to put you into the next one, which feels a bit like q doesn't work, while it actually works as intended.

---EDIT---

Maybe this will help…

Given the following sample:

aaapat1bbbpat2 pat2bbb;
aaapat1bbbpat2 pat2bbb;
aaapat1bbbpat2 pat2bbb;
aaapat1bbbpat2 pat2bbb;
aaapat1bbbpat2 pat2bbb;

the following command

:g/pat1/s/pat2/rep/gc

will create 6 nested loops:

l1
    pat2
    pat2
l2
    pat2
    pat2
l3
    pat2
    pat2
l4
    pat2
    pat2
l5
    pat2
    pat2
  • An outer loop with five iterations, one for each matching lines.
  • Five inner loops, one for each iteration of the outer loop, each with two iterations.

In other words, you have:

  • one ":g loop", that you can interrupt with <c-c> at any point,
  • and five ":s loops", that you can quit individually with q,

not one single ":s loop" as you seemed to expect.

q being solely handled by :s, pressing it at any point will quit the current inner loop as expected and, since there is nothing left to do in the current iteration of the outer loop, move to the next one and enter its corresponding inner loop.

If your outer loop has five iterations, then it executes five separate :s commands and you must press q five times to quit each of them in sequence. The other relevant commands in that /c prompt work the same so…

  • If you want the current substitution to be the last, then you will need to press l once, and then press q as many times as there are iterations left in the outer loop.
  • If you want to accept the current substitution and the following ones, then you will need to press a once for the current substitution and again for the first substitution of each remaining inner loop.

In short:

  • the /c prompt only works in :s,
  • q or a or l in that prompt only have effect for the current :s,
  • :g doesn't have anything even remotely similar,
  • :g can be interrupted by <C-c>,
  • a single :%s seems more appropriate for your use case anyway.
3
  • In my tests <C-c> has the same effect as q in the example mentionned. Do you experience something else? Dec 17, 2023 at 19:29
  • I experience the expected behavior: the loop is Interrupted.
    – romainl
    Dec 17, 2023 at 19:36
  • I have the same behavior but not for gVim on Windows. Dec 17, 2023 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.