Find and replace with a newline in Visual Studio C

2019-01-12 18:22发布

问题:

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?

回答1:

Update: According to the release notes, this was finally added in version 0.10.6

In regex mode:

  • Can now search for ^, $ or ^$ thanks to a community contribution.
  • Can now replace with \n or \t.

Remember to check the regex toggle (the rightmost icon).


Old answer: It seems like this is not possible in the current version (0.3.0) of VScode. The replace function (ctrl+h) seems to interpret \n as text, and not a new line.

I'm sure this will get fixed in an upcomming release.



回答2:

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 Code release 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.



回答3:

In version 1.1.1:

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


回答4:

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.



回答5:

Also note, after hitting the regex icon, to actually replace "\n" text with a newline I had to use two backslashes before n as search and "\n" as replace.

(It wont let me \ \ use two backslashes in this here message...:)



回答6:

On my mac version of VS Code, I select the section, then the shortcut is cntl+j to remove line breaks.