Zadaman
Zadaman

Reputation: 11

How to use 'vi' navigation commands within a shell script

I’m trying to use ex file <<EOF vi commands to go to end of a file, go up 100 lines, and 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

Upvotes: 1

Views: 107

Answers (1)

romainl
romainl

Reputation: 196826

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'

Upvotes: 2

Related Questions