There is a typo (an extra space) in the assignment to filename
variable: filename= test.txt
. This expression is interpreted as calling test.txt
command with filename
environment variable which is set to an empty string. So you should remove the space after =
.
Regarding the Error reading input message, read the Vim documentation: :help E208
:
Vim: Error reading input, exiting...
This occurs when Vim cannot read typed characters while input is required. Vim got stuck, the only thing it can do is exit. This can happen when both stdin and stderr are redirected and executing a script that doesn't exit Vim.
So Vim thinks it read a script that didn't exit Vim. Your script enters the insert mode, enters Hello
, then Esc, and finally calls the :x
command. Everything is good, and the :x
command exits Vim, if it is entered in the command line area, of course. If you had entered ^[
as Control - V, Esc, the script would have exited the insert mode and executed the :x
command successfully. Therefore, you entered ^[
as two characters. So your real input was Hello^[:x
(and the script hadn't exited Vim):
Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.
Now if you replace the two characters ^[
with the actual escape character, and execute the script from the same directory, it will fail because of a swap file:
Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...
Vim: Finished.
(you can read a long description by running vim test.txt
). You can ignore swap files with -n
option: vim -n "$filename"
. Now the output will be:
Vim: Warning: Input is not from a terminal
It makes sense. You actually don't need the interactive version of Vim. So you should use the ex mode (-e
option), or rather improved ex mode (-E
option): vim -n -E "$filename"
.
It is also a good idea to wrap the values in single quotes, if the content is not supposed to be interpreted, and in double quotes, if the string contains shell variables: filename='test.txt'
.
The fixed version:
filename='test.txt'
vim -n -E "$filename" << COMMAND
i
Hello
^[
:x
COMMAND
(^[
is entered in Vim using Control - v, Esc).