skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 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",
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search