Skip to content

Fix: Handle vim.fn.getcompletion errors gracefully with pcall#132

Open
esmuellert wants to merge 1 commit intohrsh7th:mainfrom
esmuellert:fix/getcompletion-error-handling
Open

Fix: Handle vim.fn.getcompletion errors gracefully with pcall#132
esmuellert wants to merge 1 commit intohrsh7th:mainfrom
esmuellert:fix/getcompletion-error-handling

Conversation

@esmuellert
Copy link

Fix: Handle vim.fn.getcompletion errors gracefully

Problem

vim.fn.getcompletion() throws errors when the command line contains malformed or incomplete patterns. This is a common occurrence during normal typing and causes frequent crashes.

Reproduction cases:

  1. Unmatched braces (Git users hit this constantly):

    :CodeDiff HEAD@{

    Error: E220: Missing }

  2. Incomplete regex patterns:

    :grep /\(foo

    Error: E54: Unmatched \(

  3. Other pattern errors:

    :grep /\(foo\)@<

    Error: E869: Unknown operator

Impact:

Solution

Wrap vim.fn.getcompletion() with pcall() to catch errors gracefully.

Changes:

  • 3 lines added, minimal change
  • When getcompletion fails → return empty list (no crash)
  • When getcompletion succeeds → normal behavior (no change)

Why this is the correct fix:

  1. We cannot validate all possible malformed patterns beforehand
  2. The patterns are incomplete while typing - this is expected behavior
  3. Returning empty completion on error is the right UX
  4. This was already proposed in Fix: try to get completion silently for invalid cmdline contents (#101) #102 but never merged

Testing

Before fix:

:lua vim.fn.getcompletion('e {', 'cmdline')
" E220: Missing }. (crash)

After fix:

:lua pcall(vim.fn.getcompletion, 'e {', 'cmdline')
" Returns: true, {} (no crash, empty results)

Related Issues

Please Review

This is a simple, 3-line fix for a 3-year-old bug affecting many users in common workflows (Git commands, regex patterns, etc).

The fix has been tested by users and is already being used as a local workaround in the community.

Thank you for maintaining this essential plugin! 🙏

vim.fn.getcompletion() can throw errors when the cmdline contains
malformed or incomplete patterns (e.g., unmatched braces like 'HEAD@{}',
incomplete regex patterns like '\(foo', etc).

This causes crashes with error messages like:
- E220: Missing }
- E54: Unmatched \(
- E869: Unknown operator

This fix wraps the getcompletion call with pcall to handle errors
gracefully, returning an empty completion list instead of crashing.

Fixes hrsh7th#73
Fixes hrsh7th#101
Related to hrsh7th#102
@dismint
Copy link

dismint commented Nov 23, 2025

this would be an amazing fix, thank you for the work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when calling vim.fn.getcompletion() for cmdline has unfinished regex patterns E220: Missing } error when typing :e {

3 participants