I've sample file like:


and I'd like to remove everything till the matched line dd.

Normally in Vim I type: d/dd then Enter and it works.

When using in ex, it doesn't work as expected:

ex -s +"norm ggd/dd<cr>" +%p -cq! foo.txt

Any suggestions?


There is a better way of accomplishing the task you showed in ex mode, which is by using the :delete command and a basic cmdline-range. To delete all lines between and including the beginning of the file and the search match, you can do this:


If you don't want to include the matched line in the deletion, you can do this


The first one works by selecting a range between the first line of the file and the first line that matches the pattern dd, which is then used by the delete command. The second one is essentially the same, except there is a -1 before delete which selects the line directly above the pattern dd.

For more on this, see cmdline-ranges.

  • What is the behavior of ex if the searched-for regex /dd/ doesn't exist in the file?
    – Wildcard
    Jan 25 '16 at 9:59
  • 1
    @Wildcard the command simply doesn't do anything and the whole file is printed. Jan 25 '16 at 17:09

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.