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

107,279

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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"

Solution 5

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
Share:
107,279

Related videos on Youtube

Author by

Petko Kamenov

Updated on July 08, 2022

Comments

  • Petko Kamenov 6 months

    I want to know what is the shortcut for console.log in Visual Studio code?

  • Cristian Muscalu
    Cristian Muscalu almost 6 years
    This used to work, but it doesn't anymore, since last update maybe? Is it just me? s17.postimg.org/5mxnx4umn/2017_02_14_11h10_03.jpg
  • Sebastian Sebald
    Sebastian Sebald almost 6 years
    The above defined prefix is "log" so typing "c" will not help ;) Start typing "l" instead.
  • Cristian Muscalu
    Cristian Muscalu almost 6 years
    Oh, i forgot to mention i had it changed to work with "c". I didn't change anything, and after update it's not working anymore. Don't have the snippet anymore, but this "prefix": "c", should make it work with "c" right?
  • Sebastian Sebald
    Sebastian Sebald almost 6 years
    If everything else is correct. Yes. Note that you also require to add the snippet to each language you want to use it. So if you added it to TS it will not work in JS, vice versa.
  • Cristian Muscalu
    Cristian Muscalu almost 6 years
    Here is the new proof with using "prefix" :"c" inside a js file s23.postimg.org/a8xwzsbrv/2017_02_14_12h16_00.jpg Can you confirm that is working with you?
  • Sebastian Sebald
    Sebastian Sebald almost 6 years
    Yes. But it is the last entry in a very long list.
  • BinarySolo
    BinarySolo almost 6 years
    You're the hero I needed. Thanks!
  • Chris
    Chris almost 6 years
    Haha no worries at all
  • russiansummer
    russiansummer about 5 years
    "ctrl+shift+c" for the "key" is a bit easier to click with one hand IMO and its not already taken by another keybinding if your using the default vs code keybindings
  • Omtechguy
    Omtechguy almost 5 years
    How can i make the snippet to be first in the suggestion list? in my env, when i type log i get "log" of another lib and only then i see the snippet i just created.
  • Sebastian Sebald
    Sebastian Sebald almost 5 years
    See answer below :) "editor.snippetSuggestions": "top"
  • t-bone
    t-bone over 4 years
    I get an error saying Property editor.snippetSuggestions is not allowed though, what is that about?
  • AJ H
    AJ H about 4 years
    This is awesome!
  • Ben Clarke
    Ben Clarke about 4 years
    I seem to have a bug where typing log and pressing enter only outputs console.log(); on some occasions, and I can't figure out why? Is it just me or can others type log then enter and get a consistent console.log(); output every time?
  • nedemir
    nedemir about 4 years
    You have to wait a few milisenconds / seconds for the command line to regonise what you have typed. sometimes it laggs a bit
  • rdprado over 3 years
    thanks, on mac had to go to code->preferences->keyboard shorcuts and than click on that button, than put the {...} inside the array.
  • Aashish Chaubey
    Aashish Chaubey over 3 years
    You can alternatively click on the {} button beside the search bar to open the keybindings.json file -- Easiest way as I was not able to see the "For advanced customizations open and edit keybindings.json" message!
  • Chris22
    Chris22 over 3 years
    Perfect. +1. This is what I was looking for -- not to have to make any config changes.
  • Kris Lamote
    Kris Lamote over 3 years
    thanks man, someone should update the accepted answer so everyone else can skip 10 mins searching
  • Sweet Chilly Philly
    Sweet Chilly Philly over 3 years
    This is not answering the OP's question as this would have to be imported over every project, and is just a straight up nuisance. Having an inbuilt keybind is definitely they way to go here
  • Developer
    Developer almost 3 years
    U Saved a lot of Time Man !! Thanks
  • martinedwards
    martinedwards over 2 years
    This isn't there on my mac. I needed to do cmd+shift+p and then search for keyboard shortcuts (JSON)
  • Joel Peltonen
    Joel Peltonen over 2 years
    I think this might not be very reliable or is dependent on extensions. For me co+enter generates just the the text output and co+tab generates "confirm". Even "cons" + tab just generates "console".
  • j b
    j b over 2 years
    clg + tab gives ChannelMergerNode for me!
  • palmaone
    palmaone over 2 years
    I think you need to edit your keybindings as mentioned in the answers above ;)
  • Amir Shabani
    Amir Shabani over 2 years
    Also 'cwa' for console.warn()
  • bordeaux
    bordeaux over 2 years
    @Bossan "editor.snippetSuggestions": "top" should be placed in the settings.json file (ctrl+shift+p and type user settings) Alternatively, you can search for this property in the general user settings (Preferences > Settings) and use the dropdown menu to change it to top.
  • Joey Baruch
    Joey Baruch about 2 years
    converts to console for me
  • dziku86
    dziku86 about 2 years
    And if I want console log to appear one line under the selected text? Could this be done with a snippet?
  • Jake
    Jake almost 2 years
    Thanks. This was extremely useful. Here is my version using the clipboard, and adding a quick tag made up of the clipboard with all non-alphanumeric characters removed as an identifier incase there are multiple items logged: "snippet": "console.log(\"${CLIPBOARD/[^0-9^a-z]//gi}\", ${CLIPBOARD}$1);"
  • rofrol
    rofrol over 1 year
    No need to press ctrl+space. Suggestion appear without that.
  • ejabu
    ejabu 12 months
    as for my use case, its better to remove the first element in sequence array. "editor.action.addSelectionToNextFindMatch" need to be removed
  • Ian
    Ian 11 months
    This did not work for me on Mac VSCode 1.64.2
  • Arefe
    Arefe 10 months
    This is the best answer and very helpful. Thanks a lot.
  • Arefe
    Arefe 10 months
    @BenClarke when you type the log, look the right-side for the texts that are coming. There are several options for the log and you need to choose the correct one for the console print. I suggest using the ES6 code snippets as answered above.
  • Janis Basis Basovs
    Janis Basis Basovs 10 months
    this answer should start with 'intellisene/autocomplete' -> much better approach then keybindings.