Find \n and replace with \r\n in VS Code - visual-studio-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.

Related

How to insert new lines when editing some script with fish's built-in editor?

When inputing multiline script in fish shell, e.g. I have input these script
$ for file in *.txt
echo $file
end
and my caret is after the word end. Now I want to insert a line before it, make it like:
$ for file in *.txt
echo $file
echo "hello" // I want to insert this line
end
But I found if I move my caret up and after echo $file, and press enter(or cmd/option/ctrl+enter), it just run the entire script without inserting a new line. I have to copy them to another editor and copy back after editing.
Is there any way to do it?
Update:
I just uploaded a screen recording https://asciinema.org/a/i7pOWhpdXUu0RLVOAMjVJTXbn. In the recording, I moved my caret up to after echo and pressed option + enter, but it executed the script instead of inserting a new line
fish binds escape + newline to unconditionally insert a newline. On a Mac, you would typically press option + return. However Mac terminal emulators do not send an escape-newline by default. With iTerm2 you can request that option acts as escape, under Preferences->Profiles->Keys:
Now the binding will be active and option-return will unconditionally insert a newline.
(You could instead add a key mapping for just this case if you prefer.)
You can confirm what the terminal receives from the emulator with fish_key_reader which is installed alongside fish.
In the default bindings, Alt-Enter will always insert a new line:
> bind|fgrep \\n
bind \e\n commandline\ -i\ \\n
bind \e\r commandline\ -i\ \\n
...
Depending on your system configuration, the Enter/Return key may send either a newline character (\n) or a carriage-return character (\r), so that's why there's two entries.

Tab over last semicolon and parentheses

I've been using Visual Studio Code for a short time. Is there a option to jump over the last semicolon and parentheses with tab?
Like when I type:
alert("hellof")
My cursor would be after the f and than I want to tab over the last semicolon and parentheses.
As Blorgbeard commented, the Colonize extension skips the rest of the characters in a line, appends a semicolon to the current line, and places the cursor on a new line when you press Alt + Enter.
If you specifically want the Tab key to do this, you may have to do some keymapping/macro-making or something else of that sort.

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

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

How to find and remove the invisible characters in text file using emacs

I have a .txt file named COPYING which is edited on windows.
It contains Windows-style line breaks :
$ file COPYING
COPYING: ASCII English text, with CRLF line terminators
I tried to convert it to Unix style using dos2unix. Below is the output :
$ dos2unix COPYING
dos2unix: Skipping binary file COPYING
I was surprised to find that the dos2unix program reports it as a binary file. Then using some other editor (not Emacs) I found that the file contains a control character. I am interested in finding all the invisible characters in the file using Emacs.
By googling, I have found the following solution which uses tr :
tr -cd '\11\12\40-\176' < file_name
How can I do the same in an Emacs way? I tried the Hexl mode. The Hexl mode shows text and their corresponding ASCII values in a single buffer which is great. How do I find the characters which have ASCII values other than 11-12, 40-176 (i.e tab, space, and visible characters)? I tried to create a regular expression for that search, but it is quite complicated.
To see invisible characters, you can try whitespace-mode. Spaces and tabs will be displayed with a symbol in a different face. If the coding system is automatically being detected as dos (showing (DOS) on the status bar), carriage returns at the end of a line will be hidden as well. Run revert-buffer-with-coding-system to switch it to Unix or binary (e.g. C-x RET r unix) and they'll always show up as ^M. The binary coding system will display any non-ASCII characters as control characters as well.
Emacs won't hide any character by default. Press Ctrl+Meta+%, or Esc then Ctrl+% if the former is too hard on your fingers, or M-x replace-regexp RET if you prefer. Then, for the regular expression, enter
[^#-^H^K-^_^?]
However, where I wrote ^H, type Ctrl+Q then Ctrl+H, to enter a “control-H” character literally, and similarly for the others. You can press Ctrl+Q then Ctrl+Space for ^#, and usually Ctrl+Q then Backspace for ^?. Replace all occurrences of this regular expression by the empty string.
Since you have the file open in Emacs, you can change its line endings while you're at it. Press C-x RET f (Ctrl+X Return F) and enter us-ascii-unix as the new desired encoding for the file.
Check out M-x set-buffer-file-coding-system. From the documentation:
(set-buffer-file-coding-system CODING-SYSTEM &optional FORCE NOMODIFY)
Set the file coding-system of the current buffer to CODING-SYSTEM.
This means that when you save the buffer, it will be converted
according to CODING-SYSTEM. For a list of possible values of
CODING-SYSTEM, use M-x list-coding-systems.
So, going from DOS to UNIX, M-x set-buffer-file-coding-system unix.