Home > Back-end >  Make "cut" command work only when text is selected
Make "cut" command work only when text is selected

Time:10-29

The default behaviour of Ctrl X in VSCode in the absence of selected text is to cut the current line. I would like to configure VSCode so that Ctrl X only works if text is selected. I have tried adding the following to my keybindings.json file

[
// Cut only when selection
{
    "key": "ctrl x",
    "command": "-editor.action.clipboardCutAction"
},
{
    "key": "ctrl x",
    "command": "editor.action.clipboardCutAction",
    "when": "editorHasSelection",
},
]

but it does not seem to take effect. What is the correct way to make Ctrl X work only if text is selected?

CodePudding user response:

You can use the relatively unknown command noop (no operation) here to make this a little better:

{
    "key": "ctrl x",         // needs to go before the next command
    "command": "noop"        // 'noop' FTW, no 'when' clause necessary
},
{
    "key": "ctrl x",         // needs to go after the previous
    "command": "editor.action.clipboardCutAction",
    "when": "editorTextFocus && editorHasSelection",
}

CodePudding user response:

Thanks to @rioV8's suggestion to keyboard troubleshootting functionality, I realized that even though Ctrl X was not bound to any command, the default action by VSCode was to still cut the current line.

I have fixed this by binding Ctrl X to something that does nothing when no text is selected.

[
{
    "key": "ctrl x",
    "command": "editor.action.clipboardCutAction",
    "when": "editorTextFocus && editorHasSelection",
},

{
    "key": "ctrl x",
    "command": "cancelSelection",
    "when": "editorTextFocus && !editorHasSelection",
},
]
  • Related