Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the shortcut in Visual Studio Code for console.log

People also ask

What is Ctrl P in VS Code?

Quick file navigation# Tip: You can open any file by its name when you type Ctrl+P (Quick Open). The Explorer is great for navigating between files when you are exploring a project. However, when you are working on a task, you will find yourself quickly jumping between the same set of files.


Update Feb, 2019:

As suggested by Adrian Smith and others: If you want to bind a keyboard shortcut to create a console log statement, you can do the following:

  1. File > Preferences > Keyboard Shortcuts
  2. Above the search bar on the right you'll see this icon enter image description here Click on it. (When hovered over it it says: Open keyboard shortcuts (JSON)
  3. Add this to the JSON settings:
{
  "key": "ctrl+shift+l",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    "snippet": "console.log('${TM_SELECTED_TEXT}$1')$2;"
  }
}

Pressing CTRL+SHIFT+L will output the console snippet. Also, if you already have text selected it will be put inside the log statement.


If you rather want intellisene/autocomplete:

Go to Preferences -> User Snippets -> Choose Typescript (or whatever language you want) or a 'Global Snippet File' depending on your need. A json file should open. You can add code snippets there.

There is already a snippet for console.log commented out:

"Print to console": {
    "scope": "javascript,typescript,javascriptreact",
    "prefix": "log",
    "body": [
        "console.log('$1');",
        "$2"
    ],
    "description": "Log output to console"
}

You used to have to do this for every language, but now in the 'Global Snippet File' you can set the scope property which allows you to explicitly declare multiple languages.

Should you need the exact name of the language: check it by clicking the Select Language Mode button in the right side of the VS Code bottom toolbar. It will prompt you to select a language at the top and in the process will show the JSON name of the language in parenthesis, which you can enter in the snippet file as in the example above.


Also, you should set "editor.snippetSuggestions": "top", so your snippets appear above intellisense. Thanks @Chris!

You can find snippet suggestions in Preferences -> Settings -> Text Editor -> Suggestions


All the above answers works fine, but if you don't want to change the configuration of the visual studio code, rather want auto-completion for console.log(object); you can simply use this shortcut clg and press Ctrl+Space for suggestion and hit Enter
Note : This feature is avaliable when you install JavaScript (ES6) code snippets extension.

Similarly you have auto-completion for :

  • clg for console.log(object);
  • clo for console.log('object :', object);
  • ccl for console.clear(object);
  • cer for console.error(object);
  • ctr for console.trace(object);
  • clt for console.table(object);
  • cin for console.info(object);
  • cco for console.count(label);

    (This list continues...)

    Also, Another great extension in this regard is Turbo Console Log. I'm personally using both of these on my daily basis and enjoy their combination.

References:

  1. link for JavaScript(ES6) code snippets :

https://marketplace.visualstudio.com/items?itemName=xabikos.JavaScriptSnippets

enter image description here

  1. Preview from Visual Studio Code:

enter image description here


Type log and hit enter. It will auto-complete console.log();


The top answer by @Sebastian Sebald is perfectly fine, but hitting a similar problem (not console.log specifically, but rather it "missing") I wanted to also contribute an answer.

Your prefix is indeed working - by default its log and in your case you have changed it to c. When you type log (or c) VSCode will generate a full list of "all the things™" based on many factors (ie I don't know what factors, probably class relevance).

Things like snippets tend to gravitate towards the bottom. To bump them to the top, despite their length, add this to your settings:

"editor.snippetSuggestions": "top"

In Atom there is a nice shortcut for console.log() and I wanted the same in VS Code.

I used the solution by @kamp but it took me a while to figure out how to do it. Here are the steps I used.

  1. Go to: File > Preferences > Keyboard Shortcuts

  2. At the top of the page you will see a message that says: For advanced customizations open and edit keybindings.json

Click on link

  1. This opens two panes: the default keybindings, and your custom bindings.

Enter code in right pane

  1. Enter the code provided by @kamp

Other way is to open keybindings.json file and add your desired key combination. In my case it's:

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