0

I'm trying to run a function that is in my vimrc to insert dependencies in a php class.
The script must enter a parameter type hinting in the construct of my class and assign the result of this parameter to an attribute of the local class.
I'm using Linux and I found in some posts that these types of characters that are in the function are not used to Linux. But for Mac.

Script php before run the function:

<?php
use Something\AnotherClass;    

class MyClass {

    public function __construct(AnotherClass $anotherClass)
    {
        $this->anotherClass = $anotherClass;
    }

}

Script php after run the function:

<?php

use Something\AnotherClass;    
use Something\OtherClass;

class MyClass {

    public function __construct(AnotherClass $anotherClass, OtherClass $otherClass)
    {
        $this->anotherClass = $anotherClass;
        $this->otherClass = $otherClass;
    }

}

The vimrc function:

function! AddDependency()
    let dependency = input('Var Name: ')
    let namespace = input('Class Path: ')

    let segments = split(namespace, '\')
    let typehint = segments[-1]

    exec 'normal gg/construct^M:H^Mf)i, ' . typehint . ' $' . dependency . '^[/}^>O$this->^[a' . dependency . ' = $' . dependency . ';^[?{^MkOprotected $' . dependency . ';^M^[?{^MOuse ' . namespace . ';^M^['

    " Remove opening comma if there is only one dependency
    exec 'normal :%s/(, /(/g'

endfunction

And when I try to run this function I get:

Error detected while processing function AddDependency:
line   10:
E115: Missing quote: 'normal :%s/(, /(/g
E15: Invalid expression: 'normal :%s/(, /(/g
Press ENTER or type command to continue

What is exactly this special chars?
How can I fixed this and is there some reference to learn about this chars?

  • You might want to use normal! with the exclamation mark, in order to use only internal vim commands and not commands defined by yourself (or others). Although, it seems as if your first executed normal mode command uses a command (:H) that either you or someone else defined (e.g. in a plugin). Your last normal mode command should use the exclamation mark, at least. – Karl Yngve Lervåg Jul 15 '15 at 21:06
  • I updated the question to explain what the function needs to do. – Rafael Soufraz Jul 15 '15 at 21:37
  • Your first exec command performs four or five edits: I suggest you split the command into the separate edits, instead of having one command doing all of them. This will help greatly in locating errors. – Karl Yngve Lervåg Jul 16 '15 at 7:34
1

I think the error could be resolved if you change

    exec 'normal :%s/(, /(/g'

into

    %s/\m(, /(/eg

The reasoning is that there is no need to use normal to execute an ex command.

Disclaimer: I have not studied your function in detail, and as such I do not see entirely how it works or what it does.

  • Without exec and without normal? I tried this: exec '%s/(, /(/g', and this: '%s/(, /(/g', and this: %s/(, /(/g. But nothing works. E486: Pattern not found: (, – Rafael Soufraz Jul 15 '15 at 21:29
  • I updated the question to explain what the function needs to do. – Rafael Soufraz Jul 15 '15 at 21:37
  • 1
    On a side note: always use "magicness" qualifiers for regexps in scripts: %s/\m(, /(/g. The very fact that the magic option changes the semantics of regexps (yet it still can't make regexps "very magic") means we have to litter our regexps with \m or \v everywhere, for ever and ever, happily ever after. – lcd047 Jul 16 '15 at 4:35
  • I updated the answer slightly: I agree with @lcd047 that the \m option should be present. Further, since the pattern was not found, the substitute was not necessary. In this case, we simply ignore errors, so I added the e flag. – Karl Yngve Lervåg Jul 16 '15 at 7:33
  • 1
    @RafaelSoufraz Pattern not found is very different from Missing quote and Invalid expression. It sounds like Karl's answer solved the problem you were asking about. If you expect to find that pattern and are surprised that you don't, then that's a different question–the command seems to execute correctly. – jjaderberg Jul 16 '15 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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