Use a carriage return in VS Code replace string [duplicate] - 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.

Delete whitespace in Visual Studio Code in arbitrary selection (aka join words) (not trim trailing whitespace)

It seems like this should be a simple question, but I can't figure out how to delete all the whitespace within an arbitrary selection in Visual Studio Code (another way of saying this is to join all characters within a selection).
Note: I'm not asking how to trim trailing whitespace although this function could be used to manually to that.
It seems like there should be a built in way to do this, but if there isn't can someone point me to an extension that will do this?
I wasn't able to find one yet.
You can do it fairly easily without an extension.
Copy the selection
Ctrl + N (opens new unnamed blank document)
Paste
Ctrl + H (replace dialog), space, Ctrl + Alt + Enter (replace-all)
Now copy the update string to where you want it, and close the document.
Here's an extension (remove-whitespace-aka-join-words) I built to do just this:
https://marketplace.visualstudio.com/items?itemName=tnrich.remove-whitespace-aka-join-words
Just adding this here for 2022. I use one of these two methods. The first I don't have to remember the Regular Expression formula:
Method 1:
Sometimes this method will strip all spaces in your code (which isn't intended). If that occurs, try the next method. I often try this method first.
Select an empty space, and push CTRL + F, or Apple key + F on mac. This will highlight all matching characters, spaces.
Push the Replace All button to replace them all with nothing.
Hit CTRL + SHIFT + P' or Apple key + Shift + P', and select Format Document.
Method 2:
Found here: https://www.trainingdragon.co.uk/blog/how-to-remove-empty-lines-in-visual-studio-code/
Push CTRL + F, or Apple key + F on mac. Select the Use Regular Expression button, or push Apple key + option + R to toggle it on/off.
Add ^(\s)*$\n to the find input box.
Push the Replace All button to replace them all.
Depending on your selection you might be able by simply
Select the first space
Ctrl+D to select the next space, and repeat for all your spaces
Delete
Here is a technique that could be of more general use. Make a keybinding like so:
{
"key": "shift+alt+y", // or whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/\\s//g}" // replace spaces with nothing
},
"when": "editorTextFocus && editorHasSelection"
},
Then select your text and trigger this command. [Or make it a snippet and trigger it through the command palette Insert Snippet command.]
This could be modified to use for many things very easily - in part because often the regex will be quite simple as it will operate within already selected text.
You can also just do a Find/Replace easily:
Find: just a space
Replace: with nothing
Make your selection
Enable the Find in Selection option - the "hamburger-like icon" to the right

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.

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