Visual Studio Code multiline select until a character - visual-studio-code

I would like to select all the words at once between the backtick. In MACOSX use alt+ shift and the cursor put a multi cursor at the beginning of the words but I can't stop the selection to the "`" character where the word ends.
`apples` int(200) NOT NULL,
`bananas` int(100) NOT NULL,
`mangos` int(100) NOT NULL,
`kiwi` int(100) NOT NULL,
`raspberry` int(100) NOT NULL,
Is there a way I can select until some character is found in the line where the cursor is placed?
In this case, the expected selection is:
`apples`
`bananas`
`mangos`
`kiwi`
`raspberry`

Press command+F to open the find widget.
Type `.*?` in regex mode (.* icon).
Press command+shift+L to select all occurrences.
Press esc to close the find widget.

If there is a dedicate way to select until a specific character, I am not aware of it.
However, you have a very regular "input" here (none of the fruit words have spaces in them). If that's representative of your real scenario, then to select apples, bananas, mangos, kiwi, raspberry, then put the caret right at the beginning of "`apple`", then hold alt+shift and click the beginning of "raspberry", then press ctrl+shift+right, then shift+right (Windows and Ubuntu. I think for MacOS it's option+shift+right then shift+right, but I'm not 100% sure).
This doesn't work if the fruit words have spaces in them. In that case, if you happen to be doing this because you want to do find and replace, you can use the regular expression mode of the find and replace feature. Something like find ^ `([^`]+)` and then replace withsome usage of $1.

You can use the extension Select By
Create a keybinding:
{
"key": "shift+alt+]", // or any other combo
"command": "selectby.regex",
"when": "editorTextFocus",
"args": {
"forward": "('''|\"\"\"|'|\"|`)",
"forwardNext": "{{1}}",
"forwardInclude": false,
"forwardNextInclude": false
}
}
Place multiple cursors before the strings you want to select, and press the key binding.

If you do this selection frequently you can make a keybinding for it using this extension: Find and Transform (which I wrote). Sample keybinding - in your keybindings.json:
{
"key": "alt+d", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(?<=`)(.*)(?=`)",
"restrictFind": "selections",
"isRegex": true
},
}
For this you just need to select the lines you want changed - it can be one selection, see demo. Or you could use this option:
"restrictFind": "line",
and just put a cursor on any lines you want changed - the cursor can go anywhere on the line.
By "line":

Related

How to place a cursor after every N characters in VS Code?

Say for example I have a very long line of text in a single line in VS Code (let's pretend that the example given below is very long).
0xffffffffeeeeeeee02020202aaaaaaaa
At first I placed my cursor after the characters 0x.
(the cursor is denoted by the | character in the example below)
0x|ffffffffeeeeeeee02020202aaaaaaaa
Then I want to add more cursors after every N characters from the current cursor. In this case N is equal to 8 and I want to do this twice to add two more cursor like in the example below.
0x|ffffffff|eeeeeeee|02020202aaaaaaaa
So that after I press the following sequence of keys in the keyboard, in this case those sequence of keys are ,(space)0x I should be able to get these final result.
0x, 0x|ffffffff, 0x|eeeeeeee, 0x|02020202aaaaaaaa
After I deselect the cursors I should be getting this
0x, 0xffffffff, 0xeeeeeeee, 0x02020202aaaaaaaa
Is this possible to do in VS code?
There is a straightforward regex that can do what you want:
Find: ^0x|(.{8})(?!$)
But you have to enable the Find in Selection option and trigger the Select All Matches command yourself after entering it.
Or use a macro extension like multi-command and this keybinding to automate it:
{
"key": "alt+p",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "editor.actions.findWithArgs",
"args": {
"findInSelection": true,
"isRegex": true,
"searchString": "^0x|.{8}",
}
},
"editor.action.selectAllMatches",
"cursorRight"
]
},
}
You must select up to where you want the last cursor and then trigger the macro.
Because of a flaky implementation, you must start with the Find in Selection option disabled in the Find Widget. I haven't found a way around that.
The setting Editor > Find: Seed Search String From Selection must be set to never. Otherwise your selected text will over-ride the searchString from the macro above.
Here is the pure regex method with no extensions:
Enter ^0x|(.{8})(?!$) in your Find Widget with the regex option enabled.
^0x the first part of the string you ultimately want a cursor after.
(.{8})(?!$) select each 8-character block, but not the last - that is why there is a negative lookahead for the end of the line (?!$) - so the last 8 characters are not matched. Don't worry, there will be a cursor in front of those last 8 characters as you want. (.{8}) doesn't actually need to be in a capture group, it is just clearer to see.
Select all the text to match: 0xffffffffeeeeeeee. Stop the selection there - wherever you want the last cursor.
Enable the Find in Selection option in the Find Widget by Alt+L.
Alt+Enter to select all the find matches respecting the Find in Selection option: editor.action.selectHighlights.
Step (4) will select your matches - you should have 4 for the above string. But you don't want the matches selected you just want a cursor at the beginning of each, so do step (5):
Right arrow: this cancels each selection with a cursor at the right end of each.
Type.
You can use the extension Select By
It has a command to add a new cursor by keyboard
{
"key": "ctrl+i ctrl+alt+right", // or any other key combo
"when": "editorTextFocus",
"command": "selectby.addNewSelection",
"args": {"offset": 8}
}
Now the offset is hard coded but I will add an option to ask the user the offset.
The possibility of the context switch is already working. I have to update the README.

How to jump from the first word to fifth word in a sentence of ten words in vs code

for eg:
(file.py)
print(["a","b","C","d","E","f","g","8"])
i want to change: (Without using the mouse of course)
"C" to "c","E" to "e" and "8" to "h"
I do know about the "Ctrl + (Arrow Keys)" shortcut but that only jumps word by word.
What I mean is, jumping words using some sort of indexing with numbers using keyboard shortcuts.....
It isn't exactly the scenario I designed this extension for: Find and Transform but it works pretty well in your case.
With this keybinding (in keybindings.json):
{
"key": "alt+y", // whatever keybinding you like
"command": "findInCurrentFile",
"args": {
"find": "([A-Z0-8])",
"replace": "${1:/downcase}",
"isRegex": true,
"restrictFind": "nextSelect",
"matchCase": true
}
}
Obviously, it can't know what you want to do with the 8 but it will stop there and select it so you could change it easily and continue on to the next capitalized letter.
There is no built-in indexing way to do what you want, you'll need an extension of some sort.
You could also do a find/replace with
find: ([A-Z0-9])
replace: \L$1
with matchCase and regex options enabled and then Enter would go through them one by one and downcase them and stop on the 8 for you.

How to make ctrl word end or start recognize underscore

In vscode, ctrl + arrow will not stop at an underscore. Is there any way to change this behaviour, or is there a shortcut to select characters between two underscores?
(I have searched through available shortcuts and extensions but could not find any)
Thanks!
If you add the underscore to your wordSeparators in the setting Editor: Word Separators, then
Ctrl+rightArrow : move to the next word separator,
followed by another rightArrow to move after that underscore, then
Ctrl+Shift+rightArrow will select all word characters up to the next word separator, which might or might not be the next underscore - depends on your code.
You can use the extension Select By.
With a regular expression you can specify what you recognize as a word separator. Use the moveby.regex command. And then redefine the key binding for Ctrl+ArrowRight
To select some text based on regular expressions use command selectby.regex:
Add to your settings.json
"selectby.regexes": {
"selectUnderscores": {
"surround": "_[^_]*_"
}
}
And define a keybinding:
{
"key": "ctrl+f10", // or any other key combo
"when": "editorTextFocus",
"command": "selectby.regex",
"args": ["selectUnderscores"]
}

Setting character $ to wrap text as parentheses in vscode

Suppose following code exists.
sample text
When user double click text, then press { or (, it just wraps the text while keeping it.
sample {text}
sample (text)
But I don't know how to apply this rule to $ in VS Code Settings.
What I expect is
sample $text$
Which setting in VS Code is related to this feature?
Edit> Auto Surround
is the setting in vscode. But it only applies to quotes and brackets like (), {}, <> and [] (and possibly some other language-defined cases). You cannot change that setting to include another character like $ unfortunately.
Here is a keybinding you might try (in keybindings.json):
{
"key": "alt+4", // or whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
// "snippet": "\\$$TM_SELECTED_TEXT\\$"
// to have the text still selected after the $'s are inserted, use this
"snippet": "\\$${1:$TM_SELECTED_TEXT}\\$"
},
"when": "editorTextFocus && editorHasSelection"
},
So that any selected text will be wrapped by a $ when you select it and alt+4 (where the $ is on an English keyboard). If you do that operation a lot it might be worth it.
If you use this line instead in the snippet above:
"snippet": "$1$TM_SELECTED_TEXT$1" // or
"snippet": "$1${2:$TM_SELECTED_TEXT}$1"
then more generically select text to surround, trigger that keybinding and type whichever and how many characters you want to wrap the selection.

Regex in VSCode: case conversion in replace, modifiers \l, \L, \U, \u not working

I'm trying to replace a structure like this:
testVars.bread.componentFlour
testVars.bread.componentWater
to something like this:
testVars.dough.flour
testVars.dough.water
this happens with multiple variable names; I want to remove the component and have the first letter converted to lowercase to match CamelCase.
What I tried doing was matching testVars.bread.component(.) replacing it with testVars.dough.\l$1.
According to regex documentation, that should convert my match to lowercase. However, VSCode wants to insert \l as text.
How do I get VSCode to convert my match to lowercase?
EDIT: To clarify, this is strictly for VSCode's implementation, not a regex question itself. Matching this group in notepad++ and replacing with testVars.dough.\l\1 does exactly what I want it to do.
NEW ANSWER: !! Those case modifiers like \l and \u are being added to the find/replace functionality in vscode (both in find within one file and search across multiple files [v1.49] - see https://github.com/microsoft/vscode/pull/105101) (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_47.md#case-changing-in-regex-replace) - will be in v1.47 (find) and v1.49 (search across files).
So that your original thought to
Find : testVars.bread.component(.)
Replace : testVars.dough.\l$1
is now working.
For more on how the case modifiers work in vscode, see https://stackoverflow.com/a/62270300/836330
OLD ANSWER:
While you cannot replace with a lowercase identifier in vscode, you still have some of options.
Use a regex like (?<=testVars\.).*\.component(.*) so that testVars. is not captured - because you do not want to change its case.
Ctrl+Shift+L to select all your matches (same aseditor.action.selectHighlights).
Ctrl+Shift+P, type lowerand trigger that command (or make a keybinding for this unbound command).
Trigger your replaceAll
To speed that up you have two options. (1) Make a macro that would run steps 2, 3 and 4. Or (2) make a snippet that will transform your selections - effectively running steps 3 and 4 at once.
Snippet in your keybindings.json (not in a snippets file):
{
"key": "alt+m", // choose some keybinding
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/.*\\.component(.*)/dough.${1:/downcase}/}"
},
},
and then Ctrl+Shift+L to select all, and alt+m to trigger the above insertSnippet.
Macro approach:
Using some macro extension, here multi-command, put this into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.findReplaceLowercase",
"sequence": [
"editor.action.selectHighlights", {
"command": "editor.action.insertSnippet",
"args": {
"name": "replace and lowercase",
}
},
]
}
]
and a snippet, in one of your snippets files:
"replace and lowercase": { // this "label" is used in the macro
"prefix": "for",
"body": [
"${TM_SELECTED_TEXT/.*\\.component(.*)/dough.${1:/downcase}/}"
// "${TM_SELECTED_TEXT/(.*)/${1:/downcase}/}" // to downcase all selected text
],
"description": "replace selected text"
},
and a keybinding to trigger the macro:
{
"key": "alt+m", // choose some keybinding
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.findReplaceLowercase" },
}
In the demo I copy the find regex into the find widget (or just write it there, it doesn't matter how it gets there or where focus is) and then hit alt+m (the macro keybinding) and that is it.
Obviously, this looks like a lot of work but you could keep reusing the macro and snippet, transforming to the replace result you would like the next time. And there you can use /downcase, /upcase, /capitalize and /pascalcase all you want which you can't use in the replace field of the find/search widgets.