1

Question

Thanks to the comment from @martin-tournoij on one of my other questions. He gave me this piece of code:

:%s/0x[a-fA-F0-9]\+/\=submatch(0)->str2nr(16)/

which looks for every hex-number like 0x10 or 0x539 and replaces it with the corresponding decimal value.

Okay now why this code only works in vim and not in neovim. Neovim throws this error:

E276: Cannot use function as method: str2nr


So Why?? Where is the problem??

4
  • I am not sure if someone can answer this, but I will give it a try ^^
    – Pixelbog
    Nov 28, 2022 at 17:08
  • Something like :%s/0x[a-fA-F0-9]\+/\=str2nr(submatch(0), 16)/ should probably work, but I don't have Neovim installed so I can't test. I think Neovim ported methods from Vim only very recently. Nov 28, 2022 at 18:05
  • Also: you can use :help <error-code>: :help E267, and that often gives you more detail on the error message. str2nr() is documented as supporting the method call syntax, so either you've got an older Neovim version which doesn't support it yet, or it's a bug in Neovim Nov 28, 2022 at 18:15
  • It works, thanks :)) Wow I started to be on vi/vim exchange only like 2 weeks ago, and I have learned an incredible amount of things^^ Also funny enough, the Error Code E276 doesn't exist in neovim, only in vim ^^
    – Pixelbog
    Nov 29, 2022 at 9:42

1 Answer 1

3

In VimScript the function composition can be written in two ways, either "traditional" g(f(x)), or "method" notation f(x)->g().

However, the latter one must be defined by VimScript engine for every function particularly (that's because some functions need argument other than the first one). So availability of method notation for a particular function depends on Vim/Neovim version/patch number.

And for Neovim it could be a real pain, as it both lags behind Vim and gets updated in some major repos quite infrequently.

So if you ever encounter this error then rewrite your code using more traditional syntax for function calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.