Amir Shabani
Amir Shabani

Reputation: 4207

Vim E194: No alternate file name to substitute for '#' for using '#' to find out the length of a string

I have this one-liner command, that outputs every letter of a string in a line:

❯ a="string"; for ((i=0; i<${#a}; i++)); do echo "${a:$i:1}"; done

s
t
r
i
n
g

But if I execute the same command in VIM:

:r !a="string"; for ((i=0; i<${#a}; i++)); do echo "${a:$i:1}"; done

I get the following error:

E194: No alternate file name to substitute for '#'

How can I fix this?

I'm trying to insert those lines into VIM.

Upvotes: 0

Views: 483

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172580

In a command-line, Vim allows certain special placeholders to refer to the current (or alternate) file name. This is useful both for Ex commands (e.g. :split #) and shell commands (e.g. :! python %).

As a consequence, these characters need to be escaped (by prepending a backslash) if they should be taken literally; :help cmdline-special explains this.

In your case, it's just the # that needs to be escaped:

:r !a="string"; for ((i=0; i<${\#a}; i++)); do echo "${a:$i:1}"; done

PS: If you had just followed :help E194, it would have already hinted at the fact that the # is to blame. Vim's help is really good.

Upvotes: 2

Related Questions