Search code examples
visual-studio-codekeyboard-shortcuts

Make "cut" command work only when text is selected


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?


Solution

  • 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",
    }