2

Given file alfa.txt:

foxtrot
golf
hotel
india
juliet

I would like to insert a newline after lines 2 and 4. I have this command:

ex -sc '2pu_|4pu_|x' alfa.txt

However it is inserting lines after line 2 and 3, instead of 2 and 4:

foxtrot
golf

hotel

india
juliet
5

The commands are executed serially, which means that before the 4pu_ command is executed, a newline has already been added and your previous line 4 is now line 5.

There are at least three ways to solve this:

  1. Start from the back. This way, previous changes do not affect later changes.

  2. As suggested by Christian Brabrandt, you can add newlines based on a global command:

    :g/\v^%2l|%4l/pu _
    

    To understand this command, read about :h :global. Note: To save a keypress I've used \v. With \v, we don't need to escape the %l and |.

  3. Manually add to the number(s), i.e. do

    ex -sc '2pu_|5pu_|x' alfa.txt
    
0

Here is an example based on the comment:

ex -sc 'g/\v%2l|%4l/pu_' -cx alfa.txt

Here is a working alternative to my original command:

ex -sc '4pu_|2pu_|x' alfa.txt

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.