3

I understand that sed (stream editor) is a non-interactive editor that is useful for scripting and batch processing. I also understand that ex and it's vim invokation: ex-mode are line editors for non-interactive text processing. My question is not one of opinion regarding which is better or more useful, but rather if there is any intended use case or advantage for ex/ex-mode that sets it apart from sed. The reason I ask is to determine if I'm missing out by ignoring ex-mode in favor of sed. Thanks in advance.

5

One major difference between ex and sed is that the former (at least in the Vim implementation) reads the entire file into a buffer and operates on that. Whereas the "s" in sed stands for streaming, i.e. sed reads the input file(s) line by line, and outputs it on the go. Therefore, sed handles very large files easily, whereas ex / Vim will need your operating system's virtual address space and be considerably slower in reading and writing.

So, with sed, if you need to keep a context from previous lines, you have to explicitly join it with the pattern space, or store it in the single available hold space. Whereas in ex / Vim, you can navigate back to a previous line or use regular expressions that span multiple lines in the buffer. Also, there's not only a single hold space, but multiple named registers in Vim (called named buffers in ex). So, if a single storage space isn't enough, you have to upgrade from sed to awk (to maintain the streaming aspect), or switch over to ex / Vim.

In practice

If I already have a file open in Vim, I naturally use ex mode for certain text manipulations. :substitute is one of the most frequently used commands (at least for me). Many ex commands have a corresponding (often recursive) macro implementation; depending on the task (and personal preferences / abilities), you tend to use one or the other more often.

For automation tasks, I prefer the sed / awk / Perl tool chain; the nice thing here is that you can start out small (with sed), and once you reach the limits of its capabilities, you can pretty easily migrate to the next tool, which is more powerful (but also more complex and bigger). If this is a one-off automation task, and I already know about a nifty macro or Vim plugin that greatly simplifies the implementation, I consider using Vim as a non-interactive command-line utility in silent ex mode. For other tasks, this usually is not an option because of the dependencies (to Vim, the plugin, my personal configuration).

Summary

You can do a lot of stuff with just sed, and nothing else. If you're not a Vim user (especially if you use the other editor, and can do powerful text processing there), ex (mode) is not so important, but never far away, as its syntax is closely aligned with sed, anyway.

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.