Carles Andres' avatarHomeBlogReference
Back to reference

Exclude auto-import suggestions in Neovim LSPs

This is a reference article.
Reference articles are written with the help of AI agents, after we have managed to solve a problem.

When using component libraries like shadcn/ui, the TypeScript LSP often suggests imports from the underlying libraries (@radix-ui/*, @base-ui/*) instead of your local wrapper components (@/components/ui/*). This creates noise in code actions and can lead to inconsistent imports across your codebase.

This guide demonstrates the solution using vtsls (the default TypeScript server in LazyVim). The good news: all major TypeScript language servers support import filtering through the native TypeScript preferences API, though configuration details vary slightly. See Other TypeScript language servers for alternatives.

The problem

You type <Tooltip and trigger code actions. The LSP offers:

  1. Add import from @radix-ui/react-tooltip
  2. Add import from @base-ui/react
  3. Add import from @base-ui/react/tooltip
  4. Add import from @/components/ui/tooltip ← the one you want
  5. Add import from @base-ui/react/tooltip/provider/To...

You always want option 4, but it's buried in the list.

The solution

All TypeScript language servers inherit import filtering from the native TypeScript compiler API. The feature uses patterns to exclude unwanted import suggestions from code actions.

Configuration for vtsls

The vtsls language server supports autoImportSpecifierExcludeRegexes — a list of regex patterns that filter out matching import suggestions.

Add this to your Neovim LSP configuration:

After restarting Neovim (or running :LspRestart), the filtered imports disappear from code actions.

Regex pattern tips

The patterns use JavaScript regex syntax:

PatternEffect
"^@radix-ui"Excludes any import starting with @radix-ui
"^next/router$"Excludes exactly next/router (not next/router/something)
"^next/dist"Excludes internal Next.js paths
"/internal/"Excludes any path containing /internal/

Adding JavaScript support

If you also work with .js/.jsx files, duplicate the settings under javascript:

Full example with common exclusions

Here's a more complete configuration for shadcn/ui projects:

Verifying it works

  1. Open a TypeScript file in a project with shadcn/ui installed
  2. Type a component name that exists in both @radix-ui/* and @/components/ui/*
  3. Trigger code actions (default: gra in LazyVim, or your configured keymap)
  4. Confirm only your preferred import source appears

Other TypeScript language servers

All major TypeScript language servers support import filtering through the native TypeScript compiler API. The configuration structure varies slightly between servers.

tsserver (nvim-lspconfig)

The native TypeScript server uses init_options.preferences:

Note: tsserver uses autoImportFileExcludePatterns with glob patterns (file paths), while vtsls uses autoImportSpecifierExcludeRegexes with regex patterns (module specifiers).

typescript-language-server

Uses the same nested structure as vtsls:

typescript-tools.nvim

Uses a slightly different configuration key:

Finding your current LSP

Not sure which TypeScript server you're using?

  1. Open a TypeScript file in Neovim
  2. Run :LspInfo to see active language servers
  3. Look for names like vtsls, tsserver, typescript-language-server, or typescript-tools

Pattern formats

ServerSettingPattern Type
vtslsautoImportSpecifierExcludeRegexesJavaScript regex (^@radix-ui)
tsserverautoImportFileExcludePatternsGlob patterns (**/node_modules/@radix-ui/**)
typescript-language-serverautoImportFileExcludePatternsGlob patterns
typescript-tools.nvimautoImportFileExcludePatternsGlob patterns