On version 1.23 of visual studio code, the 'Organize imports' feature was added. This is a very helpful feature as it handles the imports itself, but I'd like to be able to configure it.
The functionalities I'd like to know if are available for this feature are:
I'm asking this questions because there is no configuration information available on VSCode page, only informing this is available.
Thanks!
To sort manually, you will need to open the Command Palette ( Ctrl+Shift+P or ⌘+Shift+P ) and find the Sort Imports command. That command can also be used with any custom keybinding ( Ctrl+Shift+O or ⌘+Shift+O are the default).
With the help of prettier and a plugin, we can easily sort imports! As a side note if you're using ESLint I have another article to sort imports using that.
Organize imports into groups: first standard library imports, then third-party imports, and finally local application or library imports. Order imports alphabetically within each group. Prefer absolute imports over relative imports. Avoid wildcard imports like from module import * .
No, these more advanced options are not supported as of VS Code 1.24.
Max line length is tracked by this issue
External imports should generally come before internal imports. If you are using absolute paths, this may not be true, see this issue
Our end goal with imports is to that you never have to manually manage your imports or even look at them, so more advanced sorting/styling is out of scope
On VSCode open ⇧⌘P or Ctrl+Shift+P then
"Preferences: Configure Language Specific Settings..."
and add
"[typescript]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
Credits to this source
There is an update to the Organize Imports
function in vscode v1.68. See v1.68 Release Note: Group-aware Organize Imports.
Organize imports for JavaScript and TypeScript lets you quickly clean up your list of imports. When run, it both removes unused imports and also sorts the imports alphabetically.
However some codebases like having some degree of manual control over how their imports are organized. Grouping external vs internal imports is one of the most common examples of this:
[Starting with this...]
// local code
import * as bbb from "./bbb";
import * as ccc from "./ccc";
import * as aaa from "./aaa";
// built-ins
import * as path from "path";
import * as child_process from "child_process"
import * as fs from "fs";
// some code...
[Organize Imports will now produce this:]
With TypeScript 4.7 however, Organize Imports is now a group-aware. Running it on the above code looks a little bit more like what you’d expect:
// local code
import * as aaa from "./aaa";
import * as bbb from "./bbb";
import * as ccc from "./ccc";
// built-ins
import * as child_process from "child_process";
import * as fs from "fs";
import * as path from "path";
// some code...
So you should be able to "group" your imports - separate by newline or comment and get them sorted only within each group.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With