Find and replace with a newline in Visual Studio Code - 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 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.

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

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

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.

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

CTRL + H, then select regex (*) and write \n
Shorter version:
CTRL+H ALT+R \n

At least for me in VS Code Version 1.62.3 on Windows and VS Code Version 1.75 on MacOS (Ventura) it is working with regex, as shown below:
Replace Menu
Windows: CTRL+R
Mac: command+option+F
Activate "Use Regular Expression (ALT + R)"
Windows: ALT+R
Mac: command+option+R
Use <> in the Search field and >\n< in the Replace field
Press "Replace All (CRTL+ALT+Enter)" with an empty field
Windows: CTRL+ALT+ENTER
Mac: command+option+R
In case of the initial question, it would look like this:
Hint:
Use [\n] or \n to find all newlines
In addition, to find newlines at the beginning of a line, you can also use ^[\n] or ^\n:

with v1.31.1 in RegEx mode the Replace All functionality is broken. clicking that button replaces only one instance

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

Related

Find \n and replace with \r\n in VS Code

I have a legacy system that only accepts Windows \r\n and I want to edit a file in VS Code that has just \n to have \r\n.
I'm trying to use a Regex replace:
But this puts literal \r in instead of the whitespace char.
I've tried putting a newline in the replacement using SHIFT+ENTER:
But this just puts in \n.
How do I change the line feed chars used and save the file in VS Code?
There's the text "LF" in the bottom bar on the right, click on it and select "CRLF". Or press Ctrl+Shift+P and enter Change End of Line Sequence.
No idea why your approach doesn't work. Nor does \x0D or \15 get recognized. I'd call it a bug.
For multiple files, on Linux, I'd do it outside of the editor, e.g., with
find somedir -name '*.someext' -exec perl -pi -e 's/\n/\r\n/' {} +
Just press Ctrl+H and select regex replace. Then start input:
Find what: ^\n
Replace with: \r\n
You can try this one
In the local searchbox (Ctrl + F) you can insert newlines by pressing Ctrl + Enter.
https://www.graef.io/find-and-replace-with-a-newline-in-visual-studio-code/
Visual Studio Code provides an option to Select End of Line Sequence in its taskbar on bottom right:
When clicked, it'll provide an option to choose between CRLF (Carriage return and Line feed: Windows default) and LF (Line feed alone: Linux supported):
Make sure to save the file once the EOL sequence is changed.

Selecting block of code in Visual Studio Code

Is there a keyboard shortcut or an extension that would allow me to select a block of code?
I'd like to select everything between curly braces, between HTML tags, etc.
Use Alt + Shift + → to expand the selection between braces or tags.
Use Alt + Shift + ← to shrink the selection between braces or tags.
Here is the Microsoft Visual C++ shortcuts cheatsheet that might help you.
Update 2019/3: this inner functionality of Visual Studio is not working very well after some updates. Alt + Shift + → now selects things including braces (which annoys me).
It is not like its behavior before. It selected things between curly braces, when I first posted this answer. I'm using Mark's answer now.
If anyone has a better solution (without an extension) now, please leave a comment.
On Mac Ctrl + Shift + → to expand the selection. Press multiple times to expand to the block.
Try the expand-region extension. It currently works for JavaScript and HTML. To select ever-increasing or decreasing scope.
A quicker way is selecting a line then expanding the selection like this:
Select line Ctrl + L
Expand selection Alt + Shift + →
Doing this inside a block (HTML element, JavaScript curly braces) will select the inner block (HTML element content, inside curly braces). Do step 2 again to select block including the container (HTML element, whole function, class, etc.)
In Visual Studio Code, there is a new option called Balance. First you can place the cursor in a suitable block. After that, you can press Ctrl + Shift + P. Type balance and it lists like below.
Now press Enter, it will select the related code block like below.
For simple use, you can add a shortcut key binding.
A real working solution:
Press Command + P and search for Select to Bracket
To bind it to a key, press the little Settings icon on the right. The "Keyboard Shortcuts" Window will appear as shown in the image. Double-click on Select to Bracket and press a Keyboard shortcut you like, for example Command + Shift + H.
Now, whenever you want to select code in a block, put your cursor inside the block and press your shortcut.
It is like magic.
⌃⇧⌘← or ⌃⇧⌘→is also useful for this purpose.
A real working solution:
Search for Select to Bracket and bind it to whatever keys you like.
It is like magic.
If you are using Java in Visual Studio Code and you don't want your block selection to include the brackets (or any other peripheral character) then do the following:
Go to Visual Studio Code settings by pressing Ctrl + ,.
Search for "Java selection range" and deselect it.

How to select current word in Visual Studio Code (VS Code)?

How to select the current word, that is where the caret is at.
Note: I am looking for the shortcut for Visual Studio Code(VS Code), the text editor, and not Visual Studio IDE.
On Mac OS: Cmd+D
On Windows & Linux: Ctrl+D
Above solved the purpose for me.
But ⌘D is defined as "editor.action.addSelectionToNextFindMatch", so if you press it more than once, it will try to search and select same word in the file which then can be used to do "multi word editing".
You are looking for Shrink/Expand Selection.
Trigger it with Shift+ Alt+Left and Shift + Alt+Right
Update:
This is now called Smart select API.
This feature uses semantic knowledge to intelligently expand selections for expressions, types, statements, classes, and imports.
It is Ctrl + D that works for me in latest Visual Studio Code on Windows.
Go to File -> Preferences -> Keyboard shortcuts, you will find this:
If you want to ctrl+w to behave the same as in Idea just go keyboard settings
Search for Expand selection. Set new shortcut cmd+w or ctrl+w depending on your OS.
Also re-bind other commands that use ctrl+w to use another shortcut that you want, for example cmd+f4
You can edit keybindings.json to avoid using UI.
Shift + Alt+Right Arrow if the word is in camelCase then you will have to click Right Arrow again to select the whole camelCase. Every time you press Right Arrow again while still holding Shift + Alt down you will select a further part of the code.
so:
first the word.
then if it's part of a camelCase then the camelCase.
then if it is in a string the whole string.
... (many other posibilities)
the whole line.
everything inside the parentheses code block
the whole file
at any given time you can go back to the last selection by clicking Left Arrow instead of Right Arrow
I don't know about CTRL + w in the old Visual Studio Code but in the JetBrains IDE's this is the equivalent to CTRL + w by holding down CTRL and clicking w to select more and holding down CTRL + Shift and clicking w to unselect.
Another possibility which helps to avoid selecting only one word in camelCase is CTRL + d this will just select the whole camelCase. This will however have the side-effect of also changing the current "find" criteria.
thanks Chandan Nayak for this extra shortcut.
An unpopular opinion: you can now have Resharper keybindings, if you come from Jetbrain's camp.
The Ctrl+W expansion grow and shrinks is different from expansion selection.
On "File/Preferences/Keyboard Shortcuts" I deleted the shortcut "Ctrl + W" to close the current tab action, because for this "Ctrl+F4" works for me.
Update (14 days later): Yesterday I installed VSCode 1.34.0 - I think since then the functionality is "Ctrl + D". I was very suprised.
For any editor, you can use the below shortcuts. These shortcuts work for every text area also.
Ctrl + Shift + LeftArrow/RightArrow - this will select text word by word
Shift + UpArrow/DownArrow - this will select text line by line
Ctrl + BackSpace - this will delete text word by word
Additional
in intellijIdea
Ctrl + w - use for the select current word, after giving second Ctrl + W it will select the second word also. Like that you can select the whole line.
Ctrl + d - you can duplicate current line.

Use a carriage return in VS Code replace string [duplicate]

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 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.
In version 1.1.1:
Ctrl+H
Check the regular exp icon .*
Search: ><
Replace: >\n<
Also note, after hitting the regex icon, to actually replace \n text with a newline, I had to use \\n as search and \n as replace.
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.
On my mac version of VS Code, I select the section, then the shortcut is Ctrl+j to remove line breaks.
CTRL + H, then select regex (*) and write \n
Shorter version:
CTRL+H ALT+R \n
At least for me in VS Code Version 1.62.3 on Windows and VS Code Version 1.75 on MacOS (Ventura) it is working with regex, as shown below:
Replace Menu
Windows: CTRL+R
Mac: command+option+F
Activate "Use Regular Expression (ALT + R)"
Windows: ALT+R
Mac: command+option+R
Use <> in the Search field and >\n< in the Replace field
Press "Replace All (CRTL+ALT+Enter)" with an empty field
Windows: CTRL+ALT+ENTER
Mac: command+option+R
In case of the initial question, it would look like this:
Hint:
Use [\n] or \n to find all newlines
In addition, to find newlines at the beginning of a line, you can also use ^[\n] or ^\n:
with v1.31.1 in RegEx mode the Replace All functionality is broken. clicking that button replaces only one instance
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

Eclipse shortcut "go to line + column"

Does anyone know if there is the shortcut "go to line + column" in Eclipse?
Ctrl+L Jump to Line Number. To hide/show line numbers, press ctrl+F10 and select 'Show Line Numbers'
There is no way to go to a particular column according to my knowledge.
On OSX, the shortcut is ⌘ + L
It you want more short-cuts, refer http://www.shortcutworld.com/en/win/Eclipse.html
As you are aware CTRL+L goes to a particular Line, there is also CTRL+Q to go to the last edit location. The is no key combination in Eclipse to go to a particular Line and Column.
You can use a Keyboard Macro (available as hardware in some keyboards and included as software with some other keyboards). An explanation for Microsoft is here: http://support.microsoft.com/kb/237179 .
A Program like Autohotkey http://www.autohotkey.com/ can also be used.
You can use Ctrl + L to jump to specific line number
You can find a large list of eclipse shortcuts here: http://javatutorial.net/eclipse-shortcuts
You can use 'Command' + L to find line number in eclipse.
You can use CTRL + forward or backward key to jump to the next dot in the same line.
for example, in System.out.println("test"); you can switch between System, out and println by using CTRL + forward and backward key.