Exclude auto-import suggestions in Neovim LSPs
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:
- Add import from
@radix-ui/react-tooltip - Add import from
@base-ui/react - Add import from
@base-ui/react/tooltip - Add import from
@/components/ui/tooltip← the one you want - 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:
| Pattern | Effect |
|---|---|
"^@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
- Open a TypeScript file in a project with shadcn/ui installed
- Type a component name that exists in both
@radix-ui/*and@/components/ui/* - Trigger code actions (default:
grain LazyVim, or your configured keymap) - 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?
- Open a TypeScript file in Neovim
- Run
:LspInfoto see active language servers - Look for names like
vtsls,tsserver,typescript-language-server, ortypescript-tools
Pattern formats
| Server | Setting | Pattern Type |
|---|---|---|
| vtsls | autoImportSpecifierExcludeRegexes | JavaScript regex (^@radix-ui) |
| tsserver | autoImportFileExcludePatterns | Glob patterns (**/node_modules/@radix-ui/**) |
| typescript-language-server | autoImportFileExcludePatterns | Glob patterns |
| typescript-tools.nvim | autoImportFileExcludePatterns | Glob patterns |