简体   繁体   中英

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. I would like to know how to replace new line (\n) in place of some other text.

For example, I have html text like this

<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?

In the local searchbox ( ctrl + f ) you can insert newlines by pressing ctrl + enter .

局部搜索中的多行搜索图像

If you use the global search ( ctrl + shift + f ) you can insert newlines by pressing 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. 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.

在此处输入图像描述

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.

在此处输入图像描述

Since VS Coderelease 1.3 , the regex find has supported newline characters. To use this feature set the find window to regex mode and use \n as the newline character.

VS Code gif中的多行查找

In version 1.1.1:

  • Ctrl + H
  • Check the regular exp icon .*
  • Search: ><
  • Replace: >\n<

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

  • Control F for search, or Control Shift F for global search
  • Replace >< by >\n< with Regular Expressions enabled

在此处输入图像描述

A possible workaround would be to use the multi-cursor. select the >< part of your example use Ctrl + Shift + L or select all occurrences. 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. And use Ctrl + K Ctrl + D to skip a selection.

在我的 Mac 版本的 VS Code 上,我选择该部分,然后快捷键是Ctrl + j删除换行符。

CTRL + H , then select regex (*) and write \n

Shorter version: CTRL + H ALT + R \n

with v1.31.1 in RegEx mode the Replace All functionality is broken. 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:

  1. Replace Menu

    CTRL + R

  2. Activate "Use Regular Expression (ALT + R)"

    ALT + R

  3. Use [\n] or \n to find all newlines

  4. Press "Replace All (CRTL+ALT+Enter)" with an empty field

    CTRL + ALT + ENTER

在此处输入图像描述

In addition, to find newlines at the beginning of a line, use can also use ^[\n] or ^\n :

在此处输入图像描述

In case if you want to remove extra new lines from code then use this in vs code find a section

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM