[英]Find and replace with a newline in Visual Studio Code
I am trying out the new Microsoft Visual Studio Code editor in Linux Fedora environment.我正在 Linux Fedora 环境中试用新的 Microsoft Visual Studio 代码编辑器。 I would like to know how to replace new line (\n) in place of some other text.我想知道如何用换行符 (\n) 代替其他一些文本。
For example, I have html text like this例如,我有这样的 html 文本
<tag><tag>
which I would like to replace as我想替换为
<tag>
<tag>
In sublime I would use regex pattern and find "><" and replace with ">\n<" How do I accomplish this in Visual Studio Code?在崇高我会使用正则表达式模式并找到"><"并替换为">\n<"如何在 Visual Studio Code 中完成此操作?
In the local searchbox ( ctrl + f ) you can insert newlines by pressing ctrl + enter .在本地搜索框 ( ctrl + f ) 中,您可以按ctrl + enter插入换行符。
If you use the global search ( ctrl + shift + f ) you can insert newlines by pressing shift + enter .如果您使用全局搜索( ctrl + shift + f ),您可以通过按shift + enter来插入换行符。
If you want to search for multilines by the character literal, remember to check the rightmost regex icon .如果要按字符文字搜索多行,请记住检查最右边的正则表达式图标。
In previous versions of Visual Studio code this was difficult or impossible.在以前版本的 Visual Studio 代码中,这很难或不可能。 Older versions require you to use the regex mode, older versions yet did not support newline search whatsoever.旧版本要求您使用正则表达式模式,旧版本还不支持换行搜索。
With VS Code release 1.38 you can press CTRL + Enter in the editor find box to add a newline character.使用 VS Code 1.38 版,您可以在编辑器查找框中按CTRL + Enter来添加换行符。
With VS Code release 1.30 you can type Shift
+ Enter
in the search box to add a newline character without needing to use regex mode.使用 VS Code 1.30 版,您可以在搜索框中键入Shift
+ Enter
来添加换行符,而无需使用正则表达式模式。
Since VS Coderelease 1.3 , the regex find has supported newline characters.自 VS Code1.3 版以来,正则表达式查找已支持换行符。 To use this feature set the find window to regex mode and use \n
as the newline character.要使用此功能,请将查找窗口设置为正则表达式模式并使用\n
作为换行符。
In version 1.1.1:在 1.1.1 版中:
.*
检查常规 exp 图标.*
><
搜索: ><
>\n<
替换: >\n<
另请注意,在点击正则表达式图标后,要用换行符实际替换\n
文本,我必须使用\\n
作为搜索和\n
作为替换。
A possible workaround would be to use the multi-cursor.一种可能的解决方法是使用多光标。 select the >< part of your example use Ctrl + Shift + L or select all occurrences.选择示例的 >< 部分,使用Ctrl + Shift + L或选择所有匹配项。 Then use the arrow keys to move all the cursors between the tags and press enter to insert a newline everywhere.然后使用箭头键在标签之间移动所有光标,然后按回车键在任何地方插入换行符。
This won't work in all situations.这并不适用于所有情况。
You can also use Ctrl + D for select next match, which adds the next match to the selection and adds a cursor.您还可以使用Ctrl + D选择下一个匹配,这会将下一个匹配添加到选择中并添加一个光标。 And use Ctrl + K Ctrl + D to skip a selection.并使用Ctrl + K Ctrl + D跳过选择。
在我的 Mac 版本的 VS Code 上,我选择该部分,然后快捷键是Ctrl + j删除换行符。
CTRL + H , then select regex (*) and write \n CTRL + H ,然后选择正则表达式 (*) 并写入 \n
Shorter version: CTRL + H ALT + R \n更短的版本: CTRL + H ALT + R \n
with v1.31.1 in RegEx mode the Replace All functionality is broken.在 RegEx 模式下使用 v1.31.1,Replace All 功能被破坏。 clicking that button replaces only one instance单击该按钮仅替换一个实例
At least for me in VS Code Version 1.62.3 on Windows it is working with regex, as shown below:至少对我来说,在 Windows 上的 VS Code 版本 1.62.3 中,它正在使用正则表达式,如下所示:
Replace Menu替换菜单
CTRL + R CTRL + R
Activate "Use Regular Expression (ALT + R)"激活“使用正则表达式 (ALT + R)”
ALT + R ALT + R
Use [\n]
or \n
to find all newlines使用[\n]
或\n
查找所有换行符
Press "Replace All (CRTL+ALT+Enter)" with an empty field按“全部替换 (CRTL+ALT+Enter)” 与一个空字段
CTRL + ALT + ENTER CTRL + ALT + ENTER
In addition, to find newlines at the beginning of a line, use can also use ^[\n]
or ^\n
:此外,要在行首查找换行符,还可以使用^[\n]
或^\n
:
In case if you want to remove extra new lines from code then use this in vs code find a section如果您想从代码中删除额外的新行,请在 vs 代码中使用它找到一个部分
for opening the find box use ctrl + f in vs code
then find all new lines from code type this in the find box然后从代码中找到所有新行,在查找框中键入此
^\n
also only choosing Use regular expression option means the last option of the find box.也只选择使用正则表达式选项意味着查找框的最后一个选项。 After selecting this you will see all new lines on the code highlighted.选择此选项后,您将看到代码上的所有新行都突出显示。 then on replace box leave it as it is and click replace all option.然后在替换框上保持原样并单击全部替换选项。
Same as if you want to add a new line where ever you found space then on find, box provide one space than on replace box add就像你想在找到空间的地方添加一个新行然后在查找时,框提供一个空间而不是替换框添加
^\n and click return all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.