I've the following file (which is a simplification of the hosts file):

and I'm trying to invoke the following command:

ex +g/127/d -cwq file

which is supposed to remove lines consisting 127, then write and quit.

It works when using vim -e, however it doesn't work using ex/vi.

Although when printing buffer after the change, it seems the change has been made:

ex +"g/127/d|%p" file

but it also displays the following warning:

Badly formed address

I'm using original Ex v4.0 (gritter) 3/25/05. On OS X can be installed via brew install ex-vi.

Why doesn't the above command work in ex editor? And how do I make it work?


There are some problems here. First ex is made for interactive use. You can't use several -c commands. Second, and this is a specialty of the :g command, you can't separate several commands using the '|'. A :g/127/d|wq command would write and quit after the first match) This makes it effectively useless to add another command after the -c command. What works however is this traditional approach

cat <<EOF|ex /foobar

This also works with nvi. Though, I could make nvi also use

nvi -e +'g/127/d|
wq' foobar

but that is also not portable. (I don't know, why this works, however).

  • Perl could be used as an alternative to avoid interactive mode: $ perl -i -ne 'print unless /127/;' file - it has the advantage of being installed on most systems by default.
    – mMontu
    Jan 7 '16 at 18:02
  • or use sed, which should be also available on each POSIX system Jan 7 '16 at 20:07
  • You can use several -c commands in some implementations (not required by POSIX specifications for ex), but I believe in all such implementations you must use -s to set ex to "silent mode, in preparation for batch processing."
    – Wildcard
    Jan 25 '16 at 9:29
  • For a one-liner, you can use printf instead of cat: printf '%s\n' g/127/d x | ex /foobar
    – Wildcard
    Apr 20 '17 at 22:44

If it works with the Vim Ex mode but fails with Ex v4.0 (gritter) them it is probably an issue with the latter application. I would suggest reporting on the project bug tracker, but it seems that it isn't supported anymore.

If the command works with vim -e, wouldn't it be better to just stick with it?

  • POSIX doesn't allow several -c commands, also I believe the ex-vi project is dead Jan 7 '16 at 17:53

For full POSIX compatibility (i.e. portability), try:

ex -sc 'g/^127/d
x' file

I improved your regex slightly because it appeared to me that your intent was to only delete lines beginning with "127"; if that was incorrect, remove the ^ carat.

The -s command causes ex to run silently.

The global command must be terminated by a newline.

+cmd is not guaranteed by POSIX; only -c cmd is guaranteed, and multiple -c flags are not necessarily supported.

x is the "exit" command; it saves changes and exits, but only writes to the file if changes were actually made. (So if no changes were made, the timestamp on the file will be unaffected—which is preferable in many situations.)

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.