Vim: error reading input
Asked Answered
P

4

5

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?

Parrot answered 29/10, 2016 at 13:19 Comment(0)
B
8

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

Bertle answered 29/10, 2016 at 16:36 Comment(3)
Thank you so much, i'll put all of this into my head :DParrot
I was able to insert the unicode ESC character in place of ^[Bahr
How could I issue a command to move the cursor?Bahr
L
1

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
Lagging answered 14/3, 2022 at 2:39 Comment(0)
S
0

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.

Succession answered 29/10, 2016 at 15:11 Comment(2)
Thanks man! i wrote the ^[ using the 2 characters. I've to learn better Vim commands too :)Parrot
Than you can accept the answer. I have used the here-construction for editing files a lot, but I switched to sed -i. For interactive editing I am still using vi(m).Succession
L
0

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.

Lykins answered 29/10, 2016 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.