zed icon indicating copy to clipboard operation
zed copied to clipboard

Key Binding for "Organize Imports"

Open zauni opened this issue 2 years ago • 2 comments

Check for existing issues

  • [X] Completed

Describe the feature

I don't know if this is already possible, but it looks like we can not bind a key to the code action "Organize Imports".

In my TS files I would like to have a key binding to quickly organize the imports without opening the code action menu. Currently I have a key binding to format the code ("alt-shift-f": "editor::Format") and in this style would it be possible to have "alt-shift-o": "editor::OrganizeImports" or something like that?

This feature request is similiar to zed-industries/zed#5232 but refers to a action on save.

If applicable, add mockups / screenshots to help present your vision of the feature

No response

zauni avatar Apr 04 '23 06:04 zauni

This is a very useful feature

mirageN1349 avatar Jan 25 '24 20:01 mirageN1349

I'd also love this feature.

fredrikburmester avatar Jan 26 '24 13:01 fredrikburmester

Good want. Hope to have this feature in the near future

monmush avatar Jan 30 '24 04:01 monmush

need! For now: is there at least an automation to to this at save?

klausbreyer avatar Feb 23 '24 09:02 klausbreyer

After https://github.com/zed-industries/zed/pull/7860 we now have code_actions_on_format, which landed in 0.124.0. With that you should be able to add the following to your settings:

{
  "languages": {
    "TSX": {
      "code_actions_on_format": {
        "source.organizeImports": true
      }
    }
  }
}

And it then runs that code action whenever you trigger format:

https://github.com/zed-industries/zed/assets/1185253/2c802df6-8ca9-4c1f-b645-8c100bf247cd

I'm closing this ticket, because I'm pretty sure that this fixes the problem, even though it didn't add a new keybind. (You can just run format and it'll trigger the code actions, and format you can bind to keys). Let's create new issues if this still doesn't solve your problem :)

mrnugget avatar Feb 23 '24 12:02 mrnugget

Hi, is this not supported for Python? I already add this settings in:

  • Global settings
  • Project settings

Like this: { "languages": { "Python": { "code_actions_on_format": { "source.organizeImports": true } } } }

Or, like this: { "language_overrides": { "Python": { "code_actions_on_format": { "source.organizeImports": true } } } }

Still doesn't sort my imports. I use it alongside format_on_save config with external command (black formatter).

aarroisi avatar Apr 22 '24 03:04 aarroisi