When I start vi/vim as ex, I can use the -c option to provide the set of ex commands to apply to a file:

echo Yes | ex -sc 's/Y/y/|%p|q!' /dev/stdin

But how can I use normal mode commands like the ~ (switch case command) with ex?

  • 1
    If you want to switch case you could use :h g~ (e.g. :norm! g~2W) and not use visual mode.
    – statox
    Commented Sep 18, 2018 at 7:04
  • Thanks, @statox. Indeed running normal mode command is what I was looking for. However, could you post an answer with the full command? I tried a couple of variations around echo 'Élève' | ex -sc ':norm! g~|%p|q!' /dev/stdin but it doesn't work. Commented Sep 18, 2018 at 8:44

1 Answer 1


Based on a comment by @statox, I found a solution:

echo 'Élève' | ex -sc 'norm! g~w' -c '%p|q!' /dev/stdin

The norm command allows executing Normal mode commands. And I had to use two -c option since, after norm, the rest of the line is considered part of the normal-mode command to execute.

  • 1
    Yup I couldn't answer your comment in time but that's what I meant :) Don't forget to mark your answer as accepted when you can so that the question has an answer. Also for this kind of manipulation, I would rather use sed or perl substitution command rather than vim.
    – statox
    Commented Sep 18, 2018 at 10:11
  • There are some situations where, as far as I know, ex can be easier than sed. For instance, for removing a specific tag that spread across multiple lines. The ex commands '/id="todelete"' and 'dat' would delete the tag wit todelete id regardless of how many lines the tag takes.
    – ndvo
    Commented Oct 18, 2020 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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