How to convert embedded CRLF codes to their REAL newlines in Vscode? - visual-studio-code

I searched everywhere for this, the problem is that the search criteria is very similar to other questions.
The issue I have is that file (script actually) is embedded in another file. So when I open the parent file I can see the script as massive string with several \n and \r\n codes. I need a way to convert these codes to what they should be so that it formats the code correctly then I can read said code and work on it.
Quick snippet:
\n\n\n\n\nlocal scriptingFunctions\n\n\n\n\nlocal measuringCircles = {}\r\nlocal isCurrentlyCheckingCoherency
Should covert to:
local scriptingFunctions
local measuringCircles = {}
local isCurrentlyCheckingCoherency

perform a Regex Find-Replace
Find: (\\r)?\\n
Replace: \n

If you don't need to reconvert from newlines to \n after you're done working on the code, you can accomplish the trick by simply pressing ctrl-f and substituting every occurrence of \n with a new line (you can type enter in the replace box by pressing ctrl-enter or shift-enter).
See an example ctrl-f to do this:
If after you're done working on the code you need to reconvert to \n, you can add an invisible char to the replace string (typing it like ctrl-enter invisibleChar), and after you're done you can re-replace it with \n.
There's plenty of invisible chars, but I'd personally suggest [U+200b] (you can copy it from here); another good one is [U+2800] (⠀), as it renders as a normal whitespace, and thus is noticeable.
A thing to notice is that recent versions of vscode will show a highlight around invisible chars, but you can easily disable it by clicking on Adjust settings and then selecting Exclude from being highlighted.
If you need to reenable highlighting in the future, you'll have to look for "editor.unicodeHighlight.allowedCharacters" in the settings.

Related

Multiline regular expression search in Visual Studio Code

Multiline regular expression search doesn't work in VS Code version 1.27.2 .
Theoretically aaa(\n|.)*bbb should find string starting from aaa and ending bbb but it doesn't work.
The solution mentioned here Multi-line regular expressions in Visual Studio Code doesn't work as well.
Multiline search is added in v1.29 released in November 2018. See multi-line search.
VS Code now supports multiline search! Same as in the editor, a regex
search executes in multiline mode only if it contains a \n literal.
The Search view shows a hint next to each multiline match, with the
number of additional match lines.
This feature is possible thanks to the work done in the ripgrep tool
to implement multiline search.
Multiline search is coming to the Find Widget with v1.38. See multiline find "pre-release" notes.
Multi Line search in Find Widget
The Find Widget now supports multiple line text search and replace. By
pressing Ctrl+Enter, you can insert new lines into the input box.
.
Odd that it is Ctrl+Enter in the Find Widget but Shift+Enter in the Search Panel (see Deepu's answer below). Shift+Enter has other functionality when the Find Widget is focused.
yes, you could use regex for mutliple line search in VScode.
To find a multi-line text block starting from aaa and ending with the first bbb (lazy qualifier)
aaa(.|\n)+?bbb
To find a multi-line text block starting from aaa and ending with the last bbb. (greedy qualifier)
aaa(.|\n)+bbb
I have been looking for a quick way to do this, and I have come to the following:
start_text.*?(.|[\n])*?end_text
with start_text and end_text being the bounds of your multiline search.
breaking down the regex ".?(.|[\n])?":
".?" will match any characters from your start text to the end of the line. The "?" is there to ensure that if your end_text is on the same line the . wont just keep going to the end of the line regardless (greedy vs lazy matching)
"(.|[\n])" means either a character\whitespace or a new line
"*?" specifies to match 0 or more of the expression in the parentheses without being greedy.
Examples:
<meta.*?(.|[\n])*?/> will match from the beginning of all meta tags to the end of the respective tags
<script.*?(.|[\n])*?</script> will match from the beginning of all script tags to the respective closing tags
Warning:
Using .*?(.|[\n])*? with improperly or partially filled in start_text or end_text might crash VS Code. I suggest either writing the whole expression out (which doesn't cause a problem) or writing the start and end text before pasting in the regex. In any case, when I tried to delete parts of the starting and ending text VS Code froze and forced me to reload the file. That being said, I honestly could not find something that worked better in VS Code.
Without using regex.
Multi-line search is now possible in vs code version 1.30 and above without using regex.
Type Shift+Enter in the search box to insert a newline, and the search box will grow to show your full multiline query. You can also copy and paste a multiline selection from the editor into the search box.
You can find and replace in multiple lines by using this simple regex : StringStart\r\nStringEnd
For example
public string MethodA(int x)
{
var user;
}
public string MethodB(string y)
{
var user;
}
public string MethodC(int x)
{
var user;
}
public string MethodD(float x)
{
var user;
}
If you want to replace the name of user variable with customer along with method parameter name to user but only for the int ones.
Then the regex to find will be : int x)\r\nEnterBlankSpacesHereToReachTheString{\r\nEnterBlankSpacesHereToReachTheStringvar user
and regex to replace will be : int user)\r\nEnterBlankSpacesHereToReachTheString{\r\nEnterBlankSpacesHereToReachTheStringvar customer
See for reference
I had a similar issue, this works better for me:
aaa[.\n\r\t\S\s]*bbb
This includes carriage return (\r), new line (\n), tab (\t), any whitespece (\s) and any non whitespace (\S). There seems to be some redundancy putting "." and "\S" together, but it doesn't work without both in my case.
No regex way: you can copy multiline text and paste it in "Find in files" form:
result of "Replace all":
(.|\n)+? or [\s\S\r]* or [.\n\r\t\S\s]* may be understandable when viewed in isolation, but in an already complex regex expression, they can add that extra layer of complexity that makes the whole thing unmanageable.
On Windows, for files on the local disk, I find the best solution is to switch to using Notepad++. Not only does it handle multi-line out of the box, it also has a pleasant interface for multi-file search and replace, handles macros gracefully, and is quite light-weight. You can switch back to VScode as soon you have finished your regex changes. Personally, I deleted Notepad++ when I found VScode, but reinstalled it later when I found some of what Notepad++ had to offer was missing in VScode. Both are free to use! I'm sure there's an equivalent on the Mac.
If you are willing to search JavaScript, TypeScript or JSON files I can recommend my VScode extension
It allows for formatting agnostic text search and structural code search
You can find it on codeque.co or at VSCode Marketplace
Your query could look like this
aaa$$mbbb
where $$m means optional multiline set of any characters
Make sure to use text mode for this query
CodeQue can make much more than that!
The reason on this behavior is very simple.
Multiple line search isn't implemented yet.
see: Support multi-line search for Global search

How can I use the original unicode characters for the four card suits in a jupyter notebook?

If I copy and paste the four symbols from the character selection panel (I'm on macOS) they change to the following: ♠️ ♣️ ♥️ ♦️, whereas I'd like the heart and diamond to be red.
EDIT: Interestingly, i've noticed that if I type the sequence 👁‍🗨♥️, and then I hit backspace when the cursor is between those two characters, they both transform into 👁‍♥️! (the same happens with the other three)
Can someone explain what is happening?
I guess this is because your browser doesn't know about these special characters. But I think you can check this page https://www.w3schools.com/charsets/ref_utf_symbols.asp
and replace special characters with unicode codes from page
or from this page http://graphemica.com/%E2%9D%A4

Highlighting lines in emacs

I am using an IM which does not support viewing previous chat history nor can I change my IM. So I have to copy the chat into a text file everytime.
I am using Emacs.Is there some way by which I can highlight the lines which contain the participant names as below?
===> **[Vivek Kumar]**
Hi, how are you doing!
===> **[Mr X Y Z]**
fine
===> **[Vivek Kumar]**
sdfksd;fks;
EDIT 1:
Earlier when I'd access to gvim, I would use the highlight.vim plugin.
Emacs supports highlighting of lines or phrases. You can use one of highlight-lines-matching-regexp, highlight-phrase, or highlight-regexp. I'm sure there are several others too.
To try this, paste your text into the editor, press ESC-x (or Alt-x might work too) to enter the "mini-buffer" at the bottom of the editor window, and enter one of the above commands.
For example, I tried:
highlight-lines-matching-regexp and entered the regexp "\[.*\]" and made it match the names in your example. Here's a screenshot:
You can tweak this expression to fit your needs. Some links to regular expressions in emacs:
http://www.emacswiki.org/emacs/RegularExpression
This question on SO: Good tutorial + reference for Emacs search and replace?
Of course you could also use highlight-phrase and just highlight a simple string of text (instead of messing with regexes).
Here's one where I ran highlight-phrase and entered the two separate names and gave them different colors (hi-yellow and hi-red):
Finally you can then run unhighlight-regexp to clear out the highlighting.

Replacing a string in Rubymine with a string with newlines

I want to use the Search and Replace dialogue in Rubymine, or something similar to replace something like "Scenario:" with "#Desktop\nScenario"
I'm trying to replace every instance of Scenario: in a large Cucumber test suite with
#desktop
Scenario:
Any best ways to do this?
Update:
Thanks to #ryan2johnson9 comment, I realise there's now an easier option (tested on 2017.3).
By clicking on the "New Line" (or using the shortcut Ctrl+Shift+Enter / Alt+Enter), the input becomes multilines.
Original Answer
In the search and replace box, if you tick the "Regex" option you can do:
Search: "Scenario:"
Replace by: "#desktop\nScenario:"
The only trick is to tick the "Regex" option :)
Rubymine has macros (http://www.jetbrains.com/ruby/webhelp/binding-macros-with-keyboard-shortcuts.html) but I dont think they are powerful enough for this example.
It's possible that you could solve it with some elaborate feature hidden inside Rubymine, but I think it would be a lot easier to do this with a tool like perl/sed from the Terminal. If you are using Windows I assume you could search the net and find a text search/replace tool that fits your need.
In OSX I there are a bunch of Text Substitutions app too.
I would go that route since Rubymines macro tool isnt up to the task.
Here's a cheap and sleazy alternative:
Copy a newline character from between two empty lines in the file being edited. Temporarily add two empty lines if you don't have any.
Set up search/replace and enter the string you want to replace into the search text input box.
Paste the newline you just copied into the replacement text box plus whatever other text you want. You will be able to see the height of the replacement text input box grow vertically by one line due to the newline.
Perform the search/replace.
For this, the use of the Rubymine regex is optional.

Is there a quick way in Emacs to word wrapping?

Is there a quick and easy way to word wrap like "Apply Word Wrap" function of KDE's Kate?
Enter to wrapping mode = M-x auto-fill-mode
Wrap text = select text -> M-q
While the mishadoff's answer is great for default word wrapping, I once had to re-implement it because I wasn't content with the way Emacs did it, so I tried to scratch the bits of it together and here it is: http://pastebin.com/75q65hRf in case you need it.
With that bit of code you can configure what characters to wrap on, what characters terminate words, and also set exception rules for when the characters that would've otherwise break the line won't do it. It may also pad the created column on the right and on the left (I was using this function to format and display documentation text).