Reputation: 53
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 work, 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
The script fails with message: Vim: error reading input
.
Where did I am wrong?
Upvotes: 5
Views: 7894
Reputation: 11
I try the example above and I found that for the ex mode of vim vim -e
or vim -E
, you should add a '.' when you think the insert is finish or it will not work. The code is as the follow. ^[
is Ctrl-V Esc
#!/bin/sh
filename='test.txt'
vim -n -E "$filename" << COMMAND
i
Hello
.
^[
:x
COMMAND
Upvotes: 1
Reputation: 21492
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).
Upvotes: 8
Reputation: 9445
You can do like this:
#!/bin/sh
filename=test.txt
coms="iHello world^[:x ${filename}^M"
vim -c "normal $coms"
As Walter A said, you have to replace ^[
and ^M
by hitting respectively Ctrl-V Esc
and Ctrl-V Return
inside the coms
variable.
See man vim
, and in Vim :h :normal
for more help.
Upvotes: 0
Reputation: 19982
You should remove the space in filename= test.txt
, and check that you do not have spaces after the last line with COMMAND
. When you want to have Hello on the first line, remove the name line after the i
: iHello^[
.
Did you enter ^[
with CTRL-v ESC
? It must be one character.
Upvotes: 0