Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code - delete all blank lines - regex

People also ask

How do you delete multiple lines vs codes?

“vs code delete multiple lines” Code Answer'sWindows: Ctrl + Alt + Arrow Keys. Linux: Shift + Alt + Arrow Keys. Mac: Opt + Cmd + Arrow Keys.

How do you delete a line in VSCode?

In order to quickly delete a line in VSCode, you can simply press Ctrl+Shift+K keyboard combination while the cursor is being placed in the desired line. The hotkey can be changed by modifying editor. action. deleteLines key in VSCode settings.


For those who might be interested - what worked for me in version 1.3.1 (and still works in 1.33.1) to delete blank lines I used ctrl+h (find and replace) alt+r (Use regular expression)

In find box then:

\n\n

In replace box:

\n

This should make two consecutive end of line signs into one.

edited:

If you need to replace more empty lines (more than two) at once, you can use following regular expression in find box:

\n+

If you need to replace also empty lines with whitespaces, then you need to use following regular expression in find box:

\n+\s*\n

VS code is using javascript regular expressions


What also works is this regex pattern:

^\s*$\n

Then CTRL+Enter to replace all lines.

Explanation of the above pattern:

-----------------------------------------------
|  ^ | beginning of string anchor             |
-----------------------------------------------
| \s | any whitespace character               |
-----------------------------------------------
| '*'| zero or more repetitions               |
-----------------------------------------------
|  $ | end of string anchor                   |
-----------------------------------------------
| \n | new line                               |
-----------------------------------------------

Visual Studio Code 1.13.0 Linux Lite:

  • Hit CTRL+H
  • Select "Use Regular Expression"
  • Find box: ^(\s)*$\n (enter many ending \n as you need)
  • Replace box: empty
  • Click replace all

Empty lines gone!


Here's my regex, it catches all extra new lines and empty lines that contain only space,tabs etc

\n\s*\n

And I replace all matches with \n

Explanation

\n       : New Line
\s*      : Zero or more consecutive white space characters or new lines
\n       : Another New Line

P.S :Remember to choose the regex option in the search window!!


Try using ^\s*\n in the Replace dialog of VS Code -

see here