How to use vi navigation commands within shell script

Viewed 55

Im trying to use ex file <<EOF vi commands to go to end of a file, then go up 100 lines, then delete from that position to end of file. How can I use the G command to go to end of file within a shell script

#!/bin/bash
ex data1 <<EOF
G
100G
dG
w
q
EOF
1 Answers

The usual method is to use Ex commands via :help -c:

$ ex data1 -c '$-99,$d|wq'

The argument is read like this:

<range><command>|<command>

where:

  • $-99,$ is a :help range that starts 99 lines above the last line and ends on the last line,
  • :help :d cuts the given range,
  • :help :| separates that first command from the next,
  • :help :wq writes the file and quits.

Alternatively, you should be able to use several -c arguments:

$ ex data1 -c '$-99,$d' -c 'wq'
Related