Last updated: Apr 6, 2024
Reading time·4 min
If you need to find and replace text that contains a new line in the current file:
On Windows and Linux: Press Ctrl
+ f
(Windows and Linux) or Cmd
+ f
(macOS) to open the find and replace field.
Use Ctrl
+ Enter
(Windows and Linux) or Cmd
+ Enter
(macOS) to insert
a newline character into the field.
Note that you must press Ctrl
+ Enter
to insert a newline into the field.
Just pressing Enter
doesn't do it.
If you need to replace the selected text:
If you use the global Search functionality:
On Windows and Linux: Press Ctrl
+ Shift
+ f
(Windows and Linux) or
Cmd
+ Shift
+ f
(macOS) to open the search menu.
Use Shift
+ Enter
to insert a newline character into the field.
If you need to replace the selected text:
\n
characterYou can also use the \n
character to insert a newline character in find and
replace operations.
However, the Use Regular Expression functionality must be enabled.
Notice that the string in the find field is first\nsecond
(contains an \n
character).
The \n
character matches a newline. However, it can only be used to match a
newline if the Use regular expression functionality is enabled.
You can enable the functionality by clicking on the . *
button.
You can also enable regex search when using the global search functionality.
\n
character with a newlineIf you need to replace \n
character with a newline:
\\n
.\n
as the replacement.Note that regex search must be enabled by clicking on the . *
button,
otherwise, you won't be able to find the \n
characters.
\n
charactersIf you need to replace the new lines in the file with \n
characters:
\n
.\\n
as the replacement.Make sure to enable the regex search to be able to match the \n
characters.
If you need to replace the newline characters with a space:
\n
or use Ctrl
+ Enter
to insert a newline character.You can also use the Join Lines functionality from the Command Palette.
Ctrl
+ Shift
+ P
on Windows and LinuxCommand
+ Shift
+ P
on macOSHere is a short clip that demonstrates the feature.
If you need to remove all empty lines in Visual Studio Code:
Ctrl
+ f
(Windows and Linux) or Cmd
+ f
(macOS) to open the find and replace field.\n+
.\n
character as the replacement.Note that the regex search feature should be enabled by clicking on . *
.
We basically remove all empty lines by replacing two or more newline characters one after another with a single newline character.
Here is a short clip that demonstrates how removing all empty lines works.
Alternatively, you can use the ^\n
regular expression which only matches
newline characters at the beginning of a line.
If you pick this approach leave the replacement field empty.
Here is a short clip that demonstrates the process.
If your empty lines might also contain spaces or other whitespace characters
(e.g. tabs), use the following regular expression - ^(\s)*$\n
and leave the
replacement field empty.
The regular expression matches lines that start with one or more whitespace characters and end with a newline character.
Here is a clip that demonstrates the process.
The ^(\s)*$\n
regex removes all empty lines including lines that only contain
whitespace characters.
I've also written an article on how to remove the trailing spaces automatically in VS Code.
You can learn more about the related topics by checking out the following tutorials: