Is it possible?
2 options is preferable :
1) with variable
2) other string.
Thanks.
Yes, it's possible. The command is named "Replace in path" (Ctrl-Shift-R in my keyboard shortcut preferences).
You could have found it yourself by typing Ctrl-shift-A, and typing "replace": this will list all the commands containing the word "replace" in their name.
Related
I have multiple files with text in parenthesis that I need to extract from the file (or delete everything else in that file). I have a method that works, but it only works for one file. Here is an example of the kind of files I'm dealing with.
(is it on?)
[3.87595 3.87595 0 ]xsh
grestore
NDTMRY+Helvetica[8.5 0 0 -8.5 0 0 ]msf
mo
(NO)
The method I have used is as follows:
in notepad++ under the mark tab in find replace; Find: ^(.*?$ (with bookmark line checked)
Search>bookmarks>remove unbookmarked lines
Is there a way/better way to do this for multiple files at a time? In this or another language such as python.
Thanks!
Yes, it is possible to remove in multiple files lines that do not start with (.
Here is the screenshot with settings:
So, here are the instructions:
Press Ctrl+H and click Find in Files
In Find What, type ^(?!\().*\R*, keep Replace With empty
Add file masks in Filters
Select the initial directory in Directory.
Make sure Regular expression radio button is selected.
Adjust other options and hit Replace in Files button.
In VS Code I'm using the extension Replace Rules.
In settings I'm hoping to use this regex to replace any finds with their uppercase versions:
"replacerules.rules": {
"uppcase all keywords": {
"flags": "i",
"find": "(?<!\\w)(create|select|sum||drop|table|if|exists|day|group|by|order|min|max|and|else|iif|end|over|partition|distinct|desc)(?!\\w)",
"replace": "\\U$1"
}
The flag is working ok as is the Find but the replace is not changing the first captured group to Uppercase - why?
Edit
Wiktor has given the reason - that Javascript does not support this functionality in the replace clause.
If this extension will not do what I require is there a way I can save this regex for use through the command palate in VS Code?
First, a snippet approach:
{
"key": "alt+u", // whatever keybinding you want
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "${TM_SELECTED_TEXT/(?<!\\w)(create|select|sum|drop|table|if|exists|day|group|by|order|min|max|and|else|iif|end|over|partition|distinct|desc)(?!\\w)/${1:/upcase}/g}",
}
}
Snippets can transform to upper case: ${1:/upcase} means upcase the 1st capture group.
But for this to work the text you want to transform must be selected. You can simply select the entire document Ctrl+A and then trigger the snippet. You could combine those operations into a macro if you wish.
You could also make that into a snippet in a snippet file and then insert it through the Command Palette command Insert Snippet (which unfortunately seems to only filter by prefix not description - although it does show the description).
Extension demo:
This extension version uses your Search (across files) functionality, but you can limit it to the current file. Using this Search route you must accept the replacement dialog, there is no way to avoid that. But it gives you a nice command in the Command Palette Find-Transform:<your command title here> that could also be bound to a keybinding.
Or you can use its find in the current file and replace functionality - which will just perform the changes immediately.
Either way you can save these commands with search/replace regex's that support the case modifiers \\U, \\u, \\L and \\l in conjunction with capture groups of course in the replace value.
Find and Transform is the extension.
We have an old, grown project with thousands of php files and need to clean it up.
Throughout the whole project we do have a lot of function calls similar to:
trans('somestring1');
trans("SomeString2");
trans('more_string',$somevar);
trans("anotherstring4",$somevar);
trans($tx_key);
trans($anotherKey,$somevar);
All of those are embedded into the code and represent translation keys. I would like to find a way to extract all "translation keys" in all occurrences.
The PHP project is in VS Code, so a RegEx Search would be helpful to list the results.
Or I could search through the project with any other tool you would recommend
However I would also need to "export" just the strings to a textfile or similar.
The ideal result would be:
somestring1
SomeString2
more_string
anotherstring4
$tx_key
$anotherKey
As a bonus - if someone knows, how I could get the above list including filename where the result has been found - that would be really fantastic!
Any help would be greatly appreciated!
Update:
The RegEx I came up with:
/(trans)+\([^\)]*\)(\.[^\)]*\))?/gim
list the full occurrence - How can I just get the first part of the result (between Single Quotes OR between Double Quotes OR beginning with $)
See here: regexr.com/548d4
Here are some steps to get exactly what you want. Using this you can do a find and replace on your search results!
So you could do sequential regex find/replaces in the right circumstances.
The replace can be just within the search results editor and not affect the underlying files at all - which is what you want.
You can also have the replace action actually edit the underlying files if you wish.
[Hint: This technique can also make doing a find item a / replace with b in files that contain term c much easier to do.]
(1) Open a new search editor: Ctrl+Shift+P
(That command is currently unbound to a keybinding.)
(2) Paste this regex into the Search input box (with the regex option .* selected):
`(.*?)(\btrans\(['"]?)([^,'")]+)(.*)` - a relatively simple regex
regex101 demo
See my other answer for a regex to work with up to 6 entries per line:
(\s*\d+:\s)?((.*?)(\btrans\(['"]?)([^,'")]*)((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?)(.*)
(3) You will get a list of files with the search results. Now open a Find widget Shift+F in this Search editor.
(4) Put the same regex into that Find input. Regex option selected. Put $3 into the Replace field. This only replaces in this Search editor - not the original files (although that can be done if you want it in some case). Replace All.
If using the 1-6 version regex, replace with:
$1$5 $9 $13 $17 $21 $25
(5) Voila. You can now save this Search Editor as a file.
The first answer works for one desired capture per line as in the original question. But that relatively simple regex won't work if there are two or more per line.
The regex below works for up to 6 entries per line, like
trans('somestring1');
stuff trans("SomeString2"); some content trans("SomeString2a");more stuff [repeat, repeat]
But it doesn't for 7+ - you'll need a regex guru for that.
Here is the process again with a twist of using a snippet in the Search Editor instead of a Find/Replace. Using a snippet allows more control over the formatting of the final result.
(1) Open a new search editor: Ctrl+Shift+P (That command is currently unbound to a keybinding.)
(2) Paste this regex into the Search input box (with the regex option .* selected):
`((.*?)(\btrans\(['"]?)([^,'")]*)((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?((.*?)(\btrans\(['"]?)([^,'")]*))?)(.*)`
regex101 demo
(3) You will get a list of files with the search results. Now select all your results individually with Ctrl+Shift+L.
(4) Trigger this keybinding:
{
"key": "alt+i", // whatever keybinding you like
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "${TM_SELECTED_TEXT/((.*?)(\\btrans\\([\\'\\\"]?)([^,\\'\\\")]*)((.*?)(\\btrans\\([\\'\\\"]?)([^,\\'\\\")]*))?((.*?)(\\btrans\\([\\'\\\"]?)([^,\\'\\\")]*))?((.*?)(\\btrans\\([\\'\\\"]?)([^,\\'\\\")]*))?((.*?)(\\btrans\\([\\'\\\"]?)([^,\\'\\\")]*))?((.*?)(\\btrans\\([\\'\\\"]?)([^,\\'\\\")]*))?)(.*)/$4${8:+\n }$8${12:+\n }$12${16:+\n }$16${20:+\n }$20${24:+\n }$24/g}"
}
},
That snippet will be applied to each selection in your search result. This part ${8:+\n } is a conditional which adds a newline and some spaces if there is a capture group 8 - which would be a second trans(...) on a line.
Demo: (unfortunately, it doesn't properly show the Ctrl+Shift+L selecting all lines individually or the Alt+i snippet trigger)
I use Sublime 2 for developing R and PHP code, although I imagine this shortcut would be useful for other languages.
If I copy the path of a file from Windows Explorer / XYPlorer (or other source) it has backslashes for directories. When entering a path into the source code, it needs forward slashes.
Sublime has some reasonably powerful macro commands, but I cannot think of a combination that would be able to:
take the string of text on the current line
replace all instances of '\' and replace them with '/'
Here is the workflow that I envisage:
Locate my filename in Explorer and copy its path
In Sublime, write a line of code and paste in the path
Hit a keyboard shortcut, say Ctrl+Shift+\, and all back slashes are converted to forward slashes
The result:
myPath = "E:\WORK\Code\myFile.csv";
Becomes:
myPath = "E:/WORK/Code/myFile.csv";
Without running the risk of backslashes elsewhere in the file being changed (e.g. \n characters), and without having to use multiple key presses or mouse clicks.
I imagine this would be possible with Regex. Two things I am no expert in are Sublime macros or regex, so I wonder if anyone else knows the magical commands that would achieve this?
I tried this for about 15 minutes. A few things:
Sublime text 2 doesn't allow for find/replace with macros
Sublime text 3 doesn't allow for 'find in selection'
So, I think you are kind of beat right now other than writing a plugin, which would be fairly straightforward.
This works for Sublime Text 3:
Type r before the string to tell python to read the directory as a raw string.
This way all the backslashes are read as slashes instead of 'ignore next character' (default meaning of \ in python)
Example
myPath = r"E:\WORK\Code\myFile.csv"
Python should now read the \ as /
Initially we planned to have old convention of having m_ prefix for class variables. But now requirement has come to replace all the m_VaribaleName to this.variableName, i.e. remove the m_ and make the first character after m_ lowercase.
I can search and replace m_ with this. but this doesn't rename the variable's first character to lowercase. After search and replace if I use re-factoring tool to rename VariableName to variableName this also renames the property already exists with VariableName.
I am wondering is there any regex, tool, macro to make this task automated.
Resharper will highlight all such errors in a solution, and fix them individually, but I don't think it can fix all of them with a single command. Still, it's easy enough to navigate between errors that it finds.
You can do this in emacs.
Open your file (C-x C-f) and do M-x replace-regexp.
Let's say the name of the variable is Variable.
Your regexp query would replace \(V\)ariable for \,(downcase \1)ariable.
The \, tells emacs the following statement is a lisp expression.
Additionally, if you wanted to replace the m_ at the same time you could do replace m_\(V\)ariable for \,(downcase \1)ariable.
This will take care of all instances in a file at the same time and emacs does remember your last replace-regexp query so you do not have to retype it for multiple files. Furthermore, there is a way using DIRED mode to do the replace-regexp on multiple files at the same time.
Open up a directory on DIRED mode (C-x C-f DIR_NAME), mark the files you want by going over them (you can navigate using n and p) by pressing m. Once the files you want to process are marked press Q (capital-case). You will get the same regexp prompt as if you did a single file, enter the regexp, RET, the replacement, and RET. It will open up each file at a time, press ! for every file to replace all reg-exps in that file.
After that, you still have to save the files. Which you can do with C-s and then !.