English 中文(简体)
六、“干扰特性”错误
原标题:vim for loop "trailing characters" error
  • 时间:2012-01-12 19:59:53
  •  标签:
  • for-loop
  • vim

Why does this exceedingly simple function:

function! ParseAllEvents()
    let i = 1
    while i > 0 
        exec  ParseEvent( .i. ) 
        let i -= 1
    endwhile
endfunction

和/或:

function! ParseAllEvents()
    let i = 1
    while i > 0 
        ParseEvent(i)
        let i -= 1
    endwhile
endfunction

产生这一错误?

<E488:中型号:Parse Event(1)

<代码>ParseEvent(i)功能在接到指挥线要求时可处以罚款。

最佳回答

So, as we ve been discussing in the comments, it was a matter of "calling" the function by prepending a :call.

Generally a function will be evaluated anywhere an expression is expected, however this does not mean that they are evaluated directly in your script, since Vim script is just a chain of ex commands (those which begin with a colon). A function is not an ex command.

Let s come to the practical side, take a look in what the user manual says in chapter 41:

41.3 Expressions

Vim has a rich, yet simple way to handle expressions. You can read the definition here: |expression-syntax|. Here we will show the most common items.

The numbers, strings and variables mentioned above are expressions by themselves. Thus everywhere an expression is expected, you can use a number, string or variable. Other basic items in an expression are:

   $NAME        environment variable
   &name        option
   @r           register

The expressions referred here aren t the ex commands. Most of the time expressions are evaluated in the commands arguments. This is a Vim expression:

i+=1

But you cant use it in Vim script directly, since it s not an ex command. You need something like:

:let i+=1

现在检查<代码>的帮助:let:

:let {var-name} = {expr1}                               :let E18
                        Set internal variable {var-name} to the result of the
                        expression {expr1}.  The variable will get the type
                        from the {expr}.  If {var-name} didn t exist yet, it
                        is created.

We re looking for {expr1}. This means an expression is expected — that s what you need to check before using an ex command.

Back to the functions, note that the :call command then allows you to call a function in an ex context.

So if the command being used expects an expression argument, go ahead and include your functions, and other regular stuff. They will be evaluated, variables will have their value "yielded" and so on. The :execute comes handy if the command accepts a text argument. For example, if you need to move the current line to a line number stored in a variable, you can use the :m command. The help:

:[range]m[ove] {address}                        :m :mo :move E134
                        Move the lines given by [range] to below the line
                        given by {address}.

As you can see, an address is expected directly, not an expression. If you have the number in a variable called line and do this:

:m line

That s an error, because there is no line numbered line. Then you need :exec to evaluate the expression before executing it — that s what it does, takes an expression as argument, evaluated it and executed as an ex command.

:exec "m " . line
"     ^^^^^^^^^^^
"     This expression evaluates to, say, "m 14" which is then executed
问题回答

暂无回答




相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签