Esteban Rodriguez Esteban Rodriguez - 2 months ago 13
Bash Question

Error when using exec vi

#!/bin/bash

if [ $# -ne 1 ]
then
echo "USAGE:vitest filename"
else

FILENAME=$1

exec vi $FILENAME <<EOF
i
Line 1.
Line 2.
^[
ZZ
EOF

fi

exit 0


I'm trying to input the Line 1. and Line 2. with Exec vi using the here doc, and commands.

When running the script it gives me the following:

Vim(?):Warning: Input is not from a terminal
Vim: Error reading input, exiting...

Press ENTER or type command to continueVim: Finished.
Vim: Error reading input, exiting...

Vim: Finished.

Answer

You want to start vi in ex mode, with a few minor changes to the script.

vi -e "$FILENAME" <<EOF
i
Line 1.
Line 2.
.
wq
EOF

exec is almost certainly unnecessary, especially since you have an exit command following vi. exec is used to replace the current script with the given command; it is not needed simply to execute a command.


A brief history of UNIX text editors:

  • ed was the original editor, designed to work with a teletype rather than a video terminal.
  • ex was an extended version of ed, designed to take advantage of a video terminal.
  • vi was an editor that provided ex with a full-screen visual mode, in contrast with the line-oriented interface employed by ed and ex.