VIM计数/确定quickfix中的错误数

Aym*_*man 9 vim

这可能听起来很傻,但我没有在帮助中找到它.

运行后如何确定QuickFix中的错误数量:make

或者至少看看是否有任何错误,即错误> 0?

Lau*_*ves 13

您可以通过编程方式获取错误列表getqflist():

getqflist()                     *getqflist()*
        Returns a list with all the current quickfix errors.  Each
        list item is a dictionary with these entries:
            bufnr   number of buffer that has the file name, use
                bufname() to get the name
            lnum    line number in the buffer (first line is 1)
            col column number (first column is 1)
            vcol    non-zero: "col" is visual column
                zero: "col" is byte index
            nr  error number
            pattern search pattern used to locate the error
            text    description of the error
            type    type of the error, 'E', '1', etc.
            valid   non-zero: recognized error message

        When there is no error list or it's empty an empty list is
        returned. Quickfix list entries with non-existing buffer
        number are returned with "bufnr" set to zero.

        Useful application: Find pattern matches in multiple files and
        do something with them: >
            :vimgrep /theword/jg *.c
            :for d in getqflist()
            :   echo bufname(d.bufnr) ':' d.lnum '=' d.text
            :endfor
Run Code Online (Sandbox Code Playgroud)

如果您只想要总数,请使用len(getqflist()).例如:

:echo len(getqflist())
Run Code Online (Sandbox Code Playgroud)

如果您只是想以交互方式知道,:cw如果有任何错误,将在窗口中打开列表(如果已经打开并且没有错误则关闭它).该缓冲区中的行数是错误数.

  • 如果quickfix窗口包含未被识别为错误的文本,则`getqflist()`返回的列表将包含每个行的条目.因此`len(getqflist())`返回非零时你实际上可能仍然没有错误.您需要检查结果列表中的"有效"标志.使用`filter()`函数. (3认同)
  • `len(filter(getqflist(),'v:val.valid'))`将为您提供有效quickfix条目的数量.在许多情况下,它将等于条目数,但并非总是如此.查找`:help filter()`可能是一个良好的开端.;-) (3认同)