Koushik
Koushik

Reputation: 61

How to create shortcut for one line code in vs?

I want to create a shortcut key for one line code in visual code . For example I want to use the key "F4" to paste "<p class='example _class'>" this code in the code editor or the key "F6" to paste "<a href= ''>" . Basically I want to create shortcut for some line of code, so that I don't need to copy these line again and again to repeat this line in my project.

Upvotes: 0

Views: 507

Answers (2)

rioV8
rioV8

Reputation: 28663

If you insert a snippet with the keybinding you don't have to select and replace the class name but make it a placeholder, use TAB to move from tab stop to tab stop

{
  "key": "F4",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    "snippet": "<p class=\"${1:example _class}\">$0</p>"
  }
}

Upvotes: 0

Koushik
Koushik

Reputation: 61

After some research I got my answer, On the keybindings.json file paste the below code:

{
    "key": "F4",
    "command": "type",
    "args": { 
      "text": "<p class=\"example _class\">" 
    },
    "when": "editorTextFocus"
 },

I think the json code does not need any explanation. It is pretty much self explaining.

Upvotes: 1

Related Questions