skip to Main Content

I know the shortcut key that deletes a line (CTRL + SHIFT + K) but I don’t want this kind of behavior. Instead, when I press a keyboard shortcut I want it to clear the string of code inside the current line where my cursor is at and not move after the next line of code

For instance when I put my cursor on the 3rd line and I perform a keyboard shortcut:

1    
2     console.log('Text');  // Cursor here
3     console.log('2nd Text);
4     

It should only leave it blank and only clear the line of code but remain on the same line:

1    
2     // Newline/Whitespace Cursor is also here
3     console.log('2nd Text); // This does not move to the 2nd line.
4    

Scouring the internet, the only search results I get is how to delete the line(CTRL+SHIFT+K) but not clear it.

2

Answers


  1. This may be off topic, but it is possible with a compound keyboard shortcut you need to define yourself.
    Details here: https://code.visualstudio.com/docs/getstarted/keybindings

    The key is runCommands which lets you run multiple editor commands.

    This example does something similar, but not exactly what you want.

    {
      "key": "ctrl+alt+c",
      "command": "runCommands",
      "args": {
        "commands": [
          "editor.action.copyLinesDownAction",
          "cursorUp",
          "editor.action.addCommentLine",
          "cursorDown"
        ]
      }
    },
    

    I think what you want is more like

    {
      "key": "ctrl+alt+c",
      "command": "runCommands",
      "args": {
        "commands": [
          "editor.action.deleteLine",
          "editor.action.insertLineBefore",
          "cursorUp"
        ]
      },
      "when": "editorTextFocus" 
    },
    
    Login or Signup to reply.
  2. It looks like you want this keybinding:

    {
      "key": "ctrl+alt+left",            // whatever keybinding you want
      "command": "runCommands",
      "args": {
        "commands":[
          "cursorEnd",
          "deleteAllLeft"   // does not preserve any leading whitespace on the line
        ]
      }  
    }
    

    That will move the cursor to the end of the line and then delete everything on the line leaving the cursor on that same line at column 1.

    This series of commands will preserve any leading whitespace on that line:

    {
      "key": "ctrl+alt+left",
      "command": "runCommands",
      "args": {
        "commands":[
          "cursorEnd",
          "cursorHomeSelect",           // these do preserve leading whitespace
          "deleteLeft"
          // "deleteAllLeft",
        ]
      }  
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search