0

How can I search the help entry for the last errors after the execution?

Example

" try to run a command that doesn't exist
:qwerty
E492: Not an editor command: qwerty

I usually use :echo errmsg, :messages or :Messages (the latter is from scriptease plugin and loads the original messages into the quickfix list). And then go to :help E492 following this example.

Another option is tabnew | put = execute('messages')

I'm looking for a way to go directly to the help of the errors.

4
  • what is wrong with :help E492 ?
    – Maxim Kim
    Feb 12 at 1:35
  • I'm guessing if there is another way to do it, like "go to the last error" without having to type :h [error number]. Especially when there are a few errors, it would be nice to have each help page automatically opened
    – ranemirusG
    Feb 12 at 1:40
  • 1
    You can scan your messages for E.. codes and try to open corresponding help topics then. However you will need to define what are the last errors then. And how to open it if there would be 20+ errors.
    – Maxim Kim
    Feb 12 at 1:45
  • @MaximKim Yeah, the use case would be for ~4 errors. I'm learning Vimscript and find myself in this situation when running stuff, hence the question... I will try out your answer. BTW I use habamax everyday!
    – ranemirusG
    Feb 12 at 1:51

1 Answer 1

1

I am not sure why direct :help E... is not an option, but you can create a command to open last error:

func! HelpLastErr()
    if empty(v:errmsg)
        return
    endif
    let errcode = matchstr(v:errmsg, '^E\d\+')
    if empty(errcode)
        return
    endif
    execute "help" errcode
endfunc
command! HelpLastErr call HelpLastErr()

asciicast

And if you need to open several help topics (4) you will need to parse output of :messages and manage to open several help windows (by default it always reuses existing help window).

Here is the quick and dirty solution (you can refactor it if needed):

func! HelpLastErrors(num)
    " get all messages into msgs using :redir
    redir => msgs
    :silent messages
    redir END

    if empty(msgs)
        return
    endif

    " collect errors into a hashtable to get rid of duplicates
    let errs = {}
    for msg in reverse(split(msgs, "\n"))
        let err = matchstr(msg, '^E\d\+')
        if !empty(err)
            let errs[err] = 1
        endif
    endfor

    " going over hash keys but also need to limit it to provided num
    let err_num = 1
    for err in keys(errs)
        " if we opened a help window already, split it, otherwise it would be
        " reused and only the last error would be shown
        if err_num > 1
            split
        endif
        " open help topic for the err
        execute "help" err
        " opened specified number of help windows? get out!
        if err_num == a:num
            return
        endif
        let err_num += 1
    endfor
endfunc

command! HelpLastErrors call HelpLastErrors(4)


Your Answer

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

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