Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
How to wrap a line in console.log

I find myself frequetly wanting to enclose statements or lines with a console.log(...). For example if I have the following code:

let myVar = "Hello";
myVar.slice(1,3);|            // '|' means where the cursor is after typing in the last line

And from there I'd like to wrap that line in a console.log so it then becomes:

let myVar = "Hello";
console.log(myVar.slice(1,3));

Is there a short-cut that I can create in VS Code to do this? I have one extension that is supposed to do this but 80% of the time it gets it wrong (for example putting the ) after the ; and so I'd just like to do one short-cut command that always gets it right, without having to install an extension. How could I do this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you are going to have to use a macro extension to run a couple of commands in a row to do this. If you selected that line first you wouldn't need the macro extension. Using multi-command, put this into your keybindings.json:

{
  "key": "alt+q",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "cursorHomeSelect",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "${TM_SELECTED_TEXT/(.*)./console.log($1);/}"
        }
      }
    ]
  },
  "when": "editorFocus"
}

cursorHomeSelect first to select from the cursor to beginning of text on line.

(.*). Capture the rest of the line except for the last character ;. That last matched ; will not be in capture group 1 and so will not appear within the console.log...) body.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!