Languages
[Edit]
EN

VS Code - configure shortcut to insert console.log() in JavaScript or TypeScript

10 points
Created by:
ArcadeParade
666

In this short article, I whould like to explain how to create shortcut that inserts console.log() in VS Code.

The solution uses built in snippet feature.

console.log() shortcut in VS Code (Ctrl+Shift+L keys).
console.log() shortcut in VS Code (Ctrl+Shift+L keys).

Simple steps:

1. Go to: File -> Preferences -> Keyboard Shortcuts (or just press Ctrl+k  Ctrl+s),

2. In the top right corner click Open Keyboard Shortcuts (JSON) icon to see JSON editor,

3. Paste the following item:

{
  "key": "ctrl+shift+l",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    "snippet": "console.log('${TM_SELECTED_TEXT}$1')$2;"
  }
}

    4. Save configuration (you can press Ctrl+s),

    5. Open some JavaScript file, select some text, and press Ctrl+Shift+L keys.

     

    Hints to JSON "snippet" field:

    • $1 tells where carriage should be moved after snippet insert operation completed,
    • $2 tells from where Tab key pressed, inserts new tabs to source code - before $2 position it just jumps to the new positions,
    • $1 and $2 are called Tabstops - check official documentation here.

     

    References

    1. Snippets in Visual Studio Code - VS Code Docs
    Donate to Dirask
    Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
    Join to our subscribers to be up to date with content, news and offers.
    Native Advertising
    🚀
    Get your tech brand or product in front of software developers.
    For more information Contact us
    Dirask - we help you to
    solve coding problems.
    Ask question.

    ❤️💻 🙂

    Join