Luca Pierri Luca Pierri - 1 month ago 22
Bash Question

Vim: error reading input

I've already searched about this error, but i couldn't find a solution.
I'm following a basic guide to join the Linux world, and i'm trying to create a .txt file, adding some text lines in vim using that script. It should works, but not in my case.
Here my simple script(I'm using zsh, if it could be an important information):

#!/bin/sh

filename= test.txt
vim $filename << COMMAND
i
Hello
^[
:x
COMMAND


When i run this script.sh the : "Vim: error reading input" appears on my terminal.

Where did i get wrong?
Thanks.

Answer

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).

Comments