Remove unused imports and sort imports in VS Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
4 min

banner

# Remove unused imports and sort imports in VS Code

You can use a keyboard shortcut to remove the unused imports in VS Code:

  • on Windows and Linux, press Shift + Alt + O (the letter O, not zero).
  • on macOS, press Shift + Option + O (the letter O, not zero).

Here is a short clip that demonstrates how this works.

remove unused imports

The keyboard shortcut runs the Organize Imports command.

The command:

  • Removes the unused imports in the file.
  • Sorts the imports.

The command works for both JavaScript and TypeScript files.

# Using the Remove Unused Imports command

VS Code also has a Remove Unused Imports command if you only need to remove the unused imports without sorting.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type remove unused and select Remove Unused Imports.

remove unused imports command

At the time of writing the command is available for JavaScript and TypeScript.

By default, the command is not bound to a specific key combination.

You can configure one by using the keyboard shortcuts menu.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Type remove unused and double-click on the JavaScript and TypeScript Remove Unused Imports rows.

set keyboard shortcut remove unused imports

You can also select a row and click on the Pencil icon to set a keyboard shortcut.

  1. Specify your preferred key combination and hit Enter to confirm.

# Remove the unused imports using the command palette

You can also trigger the command via the Command Palette.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type organize and select Organize imports.

issue organize imports command via command palette

You can also view the keyboard shortcut for your operating system when using the Command Palette.

Here is a short clip of how this works.

organize imports using command palette

The command doesn't only remove the unused imports, it also sorts the import statements in the file.

# Changing the default keyboard shortcut of the Organize Imports command

If you need to change the default keyboard shortcut of the Organize Imports command:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Search for organize imports and double-click on the option.

change keyboard shortcut of organize imports command

  1. Double-click on the Organize Imports row, specify your preferred key combination and hit Enter to confirm.

You can also select the row and click on the pencil icon to set a keyboard shortcut for the command.

# Automatically removing the unused imports on save

If you need to automatically remove the unused imports on save:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the following properties to your settings.json file.
settings.json
"editor.formatOnSave": true, // Sort imports and automatically remove // unused imports when pressing CTRL + S "editor.codeActionsOnSave": { "source.organizeImports": true },

Make sure to remove the trailing comma if the property comes last.

If the editor.codeActionsOnSave object already exists in your settings.json file, you have to add the source.organizeImports property to the existing object.

remove unused imports on save

When the source.organizeImports property is set to true, every time you press Ctrl + S (or Cmd + S on macOS):

  • unused imports are removed
  • imports are sorted

Here is a short clip that demonstrates how this works.

remove unused imports on save

You can also set the source.fixAll property to true if you want to fix small syntactical errors automatically on save.

settings.json
"editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true, "source.fixAll": true },

Make sure to remove the trailing comma if the property comes last.

automatically fix small syntactical errors

I've also written an article on how to remove trailing spaces automatically in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.