lomine
lomine

Reputation: 1181

vs code shortcut or plugin in add import statement

Is there a shortcut or plugin in add an import statement like

import { ApolloClient } from 'apollo-client'

I'm sure I used to do it by pressing i space and then Tab which gave me

import {} from ''

the cursor would then jump to the from statement and then Tab would take me to the {}

Upvotes: 1

Views: 2559

Answers (2)

rioV8
rioV8

Reputation: 28733

You can use the extension My Code Actions

You can define the action to be always available to include:

"my-code-actions.actions": {
    "[javascript]": {
      "import ApolloClient": {
        //"diagnostics": ["\"(.*?)\" is not defined"],
        "text": "import { ApolloClient } from 'apollo-client'\n",
        "where": "afterLast",
        "insertFind": "^(import |from \\w+ import )"
      }
    }
  }

If this is related to an entry in the PROBLEMS panel (red squigle) you can add the "diagnostics" property.

Upvotes: 1

AKUMA no ONI
AKUMA no ONI

Reputation: 11869

You can add any shortcut you want to in VSCode via the keybindings.json file.

  1. Press F1
  2. When the input drops open type: "Open Keyboard Shortcuts" - It will have "(JSON)" next to it. Select the Open Keyboard Shortcuts (json) option when you see it.
  3. You should be in a JSON file. Now follow the instructions in the snippet below.
    // Inside the JSON files Curly Brackets, copy and paste the block below
    {
        // Follow the comment instructions
        {
            "key": "CTRL+SHIFT+I", // Choose the keybinding you want to use.
            "command": "type", // You will use 'type' for the command to exec
            "args": {
            "text": "import {} from ''" // enter the string that you want to auto-type
            }
        },
    },

    /* 
         If you did everything correctly you should be able to use the 
         keybinding you choose to have it auto type your text argument*/

For more help visit this Official VSCode Guide: https://code.visualstudio.com/docs/getstarted/keybindings

Upvotes: 1

Related Questions