skip to Main Content

In vscode I often add multiple console.log(n) in different parts of a function, for debugging.

Is there a way to do this?:

  1. Ctrl click to create multiple cursors
  2. Run a command to add console.log with increasing numbers, starting from 1.

For example:

console.log(1)

console.log(2)

Placed where the cursors are at?

2

Answers


  1. First, note that if you’re using VS Code’s builtin debugger, I’m pretty sure the debug console will show you what file and line a console.log originates from, so I don’t see what benefit this adds in that case. Also note that log points are a thing. For running in the integrated terminal, or outside of VS Code, sure, this might be useful.

    You could use snippets. The secret ingredient is snippet variables. You have a lot of options depending on what you want: TM_LINE_INDEX, TM_LINE_NUMBER, CURSOR_INDEX, CURSOR_NUMBER. You could even use TM_CURRENT_LINE (the contents of the line. You could place your cursors at the end or beginning of the lines you want to put "log points" at, and then use newlines in your snippet body, though you’d need to manually escape some quote characters).

    Ex. in a snippet file, define one such as this:

    "numbered console.log": {
        "scope": "javascript,typescript",
        "prefix": "log",
        "body": ["console.log("$CURSOR_INDEX")"]
    },
    
    Login or Signup to reply.
  2. That means you have to remove these lines after the debugging.

    Another way is to use Log Break Points.

    You can add one with: Run > New Breakpoint > Logpoint…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search