Tomasz Garbus
Tomasz Garbus

Reputation: 315

A VSCode shortcut/extension for predefined text substitution?

I am labelling a text dataset for Named Entity Recognition. Consider the following example:

{team:Alfreton Town} manager {manager:Nicky Law} says his players deserve huge credit for the character they have shown in their {league:Blue Square Bet Premier} relegation fight.

I have to locate all entities of various types and add enclose them in a category prefix and suffix. What I would like to do is to predefine a couple of shortcuts, for instance:

I am not very familiar with VScode extensions. Is there some plugin which would allow to define such substitutions?

Upvotes: 4

Views: 2063

Answers (1)

darcamo
darcamo

Reputation: 3493

You don't need an extension for that. You can define a snippet that replaces some selected text with something else (including the selected text).

For instance, with this snippet

"Replace selection with {team:Selection}": {
        "body": "{team:$TM_SELECTED_TEXT}",
        "prefix": "Selection",
        "description": "Insert hehehe"
}

You can select the bold text in

Alfreton Town manager Nicky Law says his players deserve huge credit for the character they have shown in their Blue Square Bet Premier relegation fight.

Then ctrl+shift+p, write "Insert Snippet" and choose you newly defined snippet. Then you get

{team:Alfreton Town} manager Nicky Law says his players deserve huge credit for the character they have shown in their Blue Square Bet Premier relegation fight.

You can also define keybindings for snippets and you actually put the snippet body directly in the keybinding definition (no need to change snippets file in that case). For that, open your keyboard shortcuts json file and put the following code there

{
    "key": "ctrl+meta+t",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
        "snippet": "{team:$TM_SELECTED_TEXT}"
    }
}

Now you can select some text and use ctrl+meta+t and the selected text will be replaced by {team:selected text}. You can easily do the same for the other cases such as ctrl+meta+m for manager or any other keybinding you might prefer.

Upvotes: 9

Related Questions