1

I have a file with random content like the following:

random content
random content
random content

If I execute the normal command with :execute "g/./normal inewstring-" I get the following result:

newstring-random content
newstring-random content
newstring-random content

This command is working exactly as I expected and there's no problem so far. However, if I try to execute :execute "g/./normal inewstring-\<C-c>ZZ" on the first original file, it will close the file before finishing the normal command, so if I open the file again to check its content I see the following:

newstring-random content
random content
random content

I don't understand why this is happening. In my view, the command :execute "g/./normal inewstring-\<C-c>ZZ" should work exactly like my first example, with the addition of saving and closing the file automatically. Am I missing something here? Why this command is not executing on all lines before closing the file?

1

What you're missing is that Vim will execute the whole command normal inewstring-\<C-c>ZZ for each line that matches the regexp. But the command ends with ZZ, which will save and quit. That's part of the normal command being executed by the :global. As such, Vim will execute it for the first line that matches, so the rest of the matches will not really get modified.

If you want to execute the inewstring- part for every line before executing the ZZ part, you can break it into two separate commands:

:execute "g/./normal inewstring-" | normal ZZ

Or, equivalently:

:execute "g/./normal inewstring-" | x

(The :x Ex command is the same as ZZ, see :help ZZ which mentions that explicitly.)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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