I'm looking to use ex mode of vim for a script I'm trying to write, but I can't seem to figure out the syntax that will allow me to write multiple commands.

My code looks something like this:

ex -c 'normal! 2gg19|^V49gg59|y' geom.inc

So this just enters into ex mode for the file geom.inc, highlights a block of text, and yanks that text block. All that I want to add is that it will close the file once it has done this, but I can't seem to figure out how to include the additional command to close the file. I know in general "|" is used to string together commands, but no combination that I have tried has worked. It typically causes it to think one of the commands is another file.

  • 2
    then manpage indicates the possibility of using "up to 10" -c options; does that help? – Jeff Schaller Jun 12 '17 at 20:33
  • @JeffSchaller that does work. I tried it earlier and wasn't getting it to work, but I think there were other issues at the time. Thank you! – Tyberius Jun 12 '17 at 21:27

I was making silly errors. As @Jeff Schaller suggests above, multiple -c prompts will allow multiple commands. So, my working example looks like this.

ex -c 'normal! 2gg19|^V49gg59|y' -cwq geom.inc

Where I enter ex mode (ex), prompt a command (-c), define a block ('normal! 2gg19|^V49gg59|y' where normal! allows the use of regular vi commands, 2gg19| means move to the 2nd row and 19th column, ^V enters visual block, 49gg59| moves to 49th row and 59th column, and y yanks the block), and the prompt another command to write and quit (-cwq).


Just strap the 'x' or 'wq' command at the end

ex -c execute 'normal! 2gg19|^V49gg59|y|x' geom.inc
  • When I put this in, it opened "normal! 2gg19|^V49gg59|y|x" as a file and geom.inc and neither closed on their own. – Tyberius Jun 12 '17 at 19:55
  • I assumed that your existing ex command that you showed was working. Does it even work without adding the x then? What output does it give in that case? – user218374 Jun 12 '17 at 19:57
  • I had an error in my code. The execute should not have been there. The only issue with your code is that the "x" also gets affected by the "normal!" and so it deletes a character rather than closing. – Tyberius Jun 12 '17 at 21:26

I tend to pipe the commands in, e.g.:

$ echo '
  0r !seq 10
  w test
' | ex
$ cat test

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.