When using autocompletion with <C-n>, if no matching string is found, vim shows the message Pattern not found into the status bar.

I tried something with this simple function:

function! TestCN()

    let l:retval = 0

    try "\i<C-n>"
    catch /.*/
        " should catch Pattern not Found here"
        let l:retval = 1

    return l:retval


but it doesn't work.

Is it possible to test <C-n> with a try-catch block to test if autocompletition should fail with "Pattern not found" (or any other way) ?

  • 2
    I think you probably wanted try :normal! i<C-n>—that said, in :help errors I cant find this error. It appears to not generate an Exxx error – D. Ben Knoble Nov 4 '19 at 20:19
  • Please give more background; what is your final goal? When a question is only about a small technical step, it's difficult to provide a good answer. If you don't tell us why you want this, it's easy to succumb to the XY problem. – Ingo Karkat Nov 5 '19 at 7:51
  • My goal is to imap <Tab> to <C-n> if I there is some autocomplete match, <Tab> otherwise. – simo-zz Nov 5 '19 at 8:23
  • 1
    @IngoKarkat The question was pretty clear to me. "Is it possible to test C-N [match failure] with a try-catch...?". Let's not fundamentally change the question after answers have been submitted. OP can open a new question if they'd like. – B Layer Nov 5 '19 at 20:31
  • While generally standing by the request in my last comment I see that, to be fair, the last parenthetical, "(or any other way)", introduces some ambiguity I didn't notice before. I read it as "or if [autocompletion] fails in some other way". One might instead see it as "Is it possible to test X with a try-catch...or [test X] any other way?". With the latter interpretation the question does become wider in scope. – B Layer Nov 6 '19 at 2:37

To actually trigger the completion dropdown I think your best bet is to use feedkeys() which more closely simulates a user entering text through a keyboard...

    call feedkeys("i\<C-n>")

This shows the completion dropdown for me. When the cursor follows text that won't result in any completion candidates I do see the "Pattern not found" message briefly at the bottom of the screen but control does not enter the catch block that I can see. Could be as @D.BenKnoble mentioned in his comment that this isn't an "official", catchable EXXX error...which kind of makes sense as this is not really a fault condition. Just a state to communicate.

  • Don't you just add your observations and tip to use feedkeys() here? Where's the answer to "how can I check for the lack of completion results"? – Ingo Karkat Nov 6 '19 at 12:20
  • @IngoKarkat Where's that question asked? In my comment above I acknowledge that I noticed, after the fact, some possible ambiguity in the question. The unambiguous part, however, is the part I answered! – B Layer Nov 6 '19 at 12:27
  • OP stated in the comments _ goal is to imap <Tab> to <C-n> if I there is some autocomplete match, <Tab> otherwise_ after I asked for more background information, as trying to :catch something isn't the end result one would be interested in. – Ingo Karkat Nov 6 '19 at 12:30
  • @IngoKarkat The comment that was added after I answered? In response to your prompting? So? You can't edit a question with comments anyways. Comments can be transient. – B Layer Nov 6 '19 at 12:32
  • @IngoKarkat Oh, and regarding "trying to :catch something isn't the end result one would be interested in", I must have missed the rule that says you can't ask technical questions about vim programming. Sorry, no, it's a baseless assertion. Mere opinion offered as fact. – B Layer Nov 6 '19 at 15:47

I agree with @D.BenKnoble's comment that Pattern not found is not strictly an error, more of an exceptional condition, and therefore doesn't have an E... number and no way to :catch.

If you use the completion menu, you could use :help pumvisible() to use the side effect of showing (or not) the menu in case of matches. Also, there's the :help CompleteDone event that you can hook into. Getting this right under all circumstances can be difficult, though. Rather than trying to implement this on your own, have a look at plugins like SuperTab; they may also suit your needs.

  • I wouldn't even classify it as an exceptional condition but, then, trying to classify these things at all is probably a fool's errand...I notice that an occurrence as common as typing /foo when there's no "foo" gets you an E486. Who knows (besides the dev(s)) what the criteria are for getting an Exxx code – B Layer Nov 6 '19 at 11:18
  • @BLayer: You search for something to go there; the E486 tells you "you can't". Whereas insert completion is more like "show me what you can offer so I can choose and save some typing or remembering". If there are no matches, that's a valid (if somewhat exceptional) result, but not an error. This is how I interpret it. – Ingo Karkat Nov 6 '19 at 12:17
  • Asking the question "does this buffer contain 'foo'" and getting the answer "no" isn't exceptional at all, IMO. It's a routine operation. Obviously it's a subjective thing but yours seems a pretty strict interpretation of things...to me. :) – B Layer Nov 6 '19 at 12:24
  • @BLayer That's what I meant... a search jumps or gives E486. – Ingo Karkat Nov 6 '19 at 12:32
  • How many other apps can you find that classify a routine search result with an error message. I see you're also a SW developer...would you do it this way? Personally, I'd at the very least avoid classifying one as error and another as something different. Beyond that, I'd make them both simple, user friendly messages and not let users get even a sniff of the concept of "error" from either. (If there's some internal, programmer facing reason to catch search miss that could be handled out of sight of average end users). – B Layer Nov 6 '19 at 12:49

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.