Move cursor up after deleting a line in VS Code
Asked Answered
S

4

14

In IntelliJ IDEA and other JetBrains IDEs there is a setting I quite enjoy: When you delete an empty line, its cursor automatically move to the upper line, like this:

enter image description here

How can I assign this setting in VS Code?

Swordplay answered 4/10, 2018 at 4:19 Comment(4)
It should be mentioned that what you are doing is actually SHIFT + DELETE in IntelliJ.Barbel
no, I press Backspace buttonSwordplay
Doesn't backspace do this in VS Code?Havildar
yes, backspace only delete to the top of the line.Swordplay
C
14

You can use editor.action.deleteLines to delete the current line by pressing Ctrl + Shift + K.

However, your cursor will not move up after the line is deleted as you have shown in IntelliJ. I don't see a built-in option for this feature right now, but you can create your own macro to do it with the macros extension. It only takes a couple minutes to configure.

  1. Install the macros extension.
  2. Add the following to your settings.json file to create a macro called deleteLinesUp that will delete the line and then move the cursor up.

    "macros": {
        "deleteLinesUp": [
            "editor.action.deleteLines",
            "cursorUp"
        ]
    },
    
  3. Open your keybindings.json file and add the following to override Ctrl + Shift + K so it calls deleteLinesUp instead of deleteLines.

    {
        "key": "ctrl+shift+k",
        "command": "macros.deleteLinesUp"
    }
    

Now it will behave as you desire. Also, you can bind this macro to whatever key combination you want.

Capsular answered 4/10, 2018 at 5:7 Comment(1)
If I want to implement this feature(delete line and move cursor up) in my developing vscode extension, how to do it ?Embarrass
O
10

You want to install the Hungry Delete extension.

It changes the behavior of the backspace. When you hit backspace in a line that has none other than white space, it erases the whole line and moves the cursor up, which is the same behavior of JetBrains' editors, demonstrated in the question.

Orphism answered 5/2, 2019 at 18:46 Comment(0)
B
1

I believe you can do this without an extension. Try this keybinding (in your keybindings.json):

{
  "key": "backspace",         // whatever you want, demo is with backspace
  "command": "runCommands",
  "args": {
    "commands": [
      "cursorHomeSelect",    // select the entire line
      "cursorHomeSelect",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "${TM_SELECTED_TEXT/^(\\s+)$//gm}"   // only match empty lines
        }
      },
      "deleteLeft"     // same as backspace
    ]
  },
  // "when": "editorTextFocus && editorLangId === json"
  "when": "editorTextFocus"    // MUST have this
}

I turned on the Render Whitespace setting so can see where the "empty lines" (those with nothing but whitespace) end.

The keybinding will remove any whitespace-only lines and move the cursor to the end of the previous line.

[If the intention is to also remove all whitespace at the end of a line, the regular expression in the snippet can probably be modified to do that to.]

delete empty lines and move cursor up

Briones answered 8/4 at 5:48 Comment(1)
very nice. I suggest you use "when": "editorTextFocus && !editorReadonly && !editorHasSelection".Potentilla
F
-1

Go to File -> Preferences -> Keyboard Shortcuts

search for "Delete All Left" and "Delete All Right"

and add your keys, I prefer to use Ctrl+Shift+Backspace and Ctrl+Shift+End

Felicidadfelicie answered 4/3, 2019 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.