简体   繁体   English

在 Visual Studio Code 中查找并替换为换行符

[英]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作为换行符。

VS Code gif中的多行查找

In version 1.1.1:在 1.1.1 版中:

  • Ctrl + H Ctrl + H
  • Check the regular exp icon .*检查常规 exp 图标.*
  • Search: ><搜索: ><
  • Replace: >\n<替换: >\n<

另请注意,在点击正则表达式图标后,要用换行符实际替换\n文本,我必须使用\\n作为搜索和\n作为替换。

  • Control F for search, or Control Shift F for global search Control F 进行搜索,或 Control Shift F 进行全局搜索
  • Replace >< by >\n< with Regular Expressions enabled><替换为>\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 中,它正在使用正则表达式,如下所示:

  1. Replace Menu替换菜单

    CTRL + R CTRL + R

  2. Activate "Use Regular Expression (ALT + R)"激活“使用正则表达式 (ALT + R)”

    ALT + R ALT + R

  3. Use [\n] or \n to find all newlines使用[\n]\n查找所有换行符

  4. 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.

相关问题 全局查找并替换Visual Studio Code中的换行符 - Global find and replace with newline in Visual Studio Code 在所选内容中的 Visual Studio 代码中查找和替换 - Find and replace in Visual Studio code in a selection Visual Studio代码查找/替换-用名称保存 - Visual Studio Code Find/replace - save with name Find and Replace with Visual Studio Code 将单元格 alignment 替换为 class - Find and Replace with Visual Studio Code to replace cell alignment with a class 如何在Visual Studio代码中替换文档中的每一个换行符,而不是每两个换行符(或更多)? - How can I replace in Visual Studio Code every one newline in a document, but not every two newlines (or more)? 如何使用查找和替换交换 Visual Studio Code 中的两个单词? - How to swap two words in Visual Studio Code with a find and replace? 我们可以在 Visual Studio Code 中查找并替换为变量吗? - Can we find and replace with a variable in Visual Studio Code? 在Visual Studio Code 1.26.1中查找和替换PHP短标签 - Find & Replace PHP Short Tags in Visual Studio Code 1.26.1 Visual Studio Code:如何自动执行简单的正则表达式查找和替换? - Visual Studio Code: How to automate a simple regex-find and replace? 在 Visual Studio Code 中查找并替换为换行符和回车符 - Find and replace with newlines and carriage returns in Visual Studio Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM