how to select real numbers by double clicking in VSCode? - visual-studio-code

because . is a word separator, a real number like 0.1 will be selected only 0 or 1.
but if I remove . from word separators, the whole method call such as a.b or a.b.c will be selected, rather than a, b or c selected.
furthermore, ' is also a possible separator between the digits, which can not be selected correctly as well.
so is there any extension that can solve this problem?

With the extension Select By v0.10.0 you can select the text surrounding the current selection described with a regular expression.
If you add the following to your settings
"selectby.regexes": {
"selectFloat": {
"surround": "[-+]?\\d+(\\.\\d+)?([eE][-+]?\\d+)?[fF]?"
}
}
Place the cursor somewhere inside the number and execute the command Select text range based on regex and select the option selectFloat from the QuickPick list.
You can add a keybinding if needed
{
"key": "ctrl+shift+f", // any key combo you like
"when": "editorTextFocus",
"command": "selectby.regex",
"args": ["selectFloat"]
}

This extension Quick and Simple Text Selection allows for selecting of everything within single quotes, double quotes, backticks, etc., although it requires keyboard shortcuts.

Related

How can I more easily replace a selection of text with an equal number of space characters?

I'm doing a lot of text editing right now that involves replacing spans of text with spaces (same number of spaces as number of replaced characters). It would be easier and more efficient if I could somehow highlight/select the text to replace, and then replace it all with blank spaces instead of first deleting the text and then manually refilling the spaces, tabs or newlines, especially during moments where I want to be precise.
For example:
This is my example sentence.
I decide I want to select 'my example' and delete it, like this:
This is sentence.
Instead of having it go like this:
This is sentence.
Is there an easier way to do this?
This is also easy with this extension, Find and Transform (disclaimer: I wrote it). Here is a keybinding that would work (in your keybindings.json):
{
"key": "alt+q", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"replace": [ "$${ return `$1`.replace(/./g, ' '); }$$" ],
"isRegex": true,
"restrictFind": "selections",
"postCommands": "cancelSelection" // if you had multiple selections and wanted to clear them
}
}
"restrictFind": "selections" will only work within selections, as many as you want. If you had repetitive text in the document, you could omit this argument and it will find the selected text wherever it occurs in the document and replace it.
The $1 referred to is the selected text.
What you can do is switch your editor find mode to regex, and put "[^\s]" in the find input, and then " " (space) in the replace input. Then select the text you want to replace with spaces and either click the "Find in Selection" button or use the keyboard shortcut (Ex. alt+l). Then click "Replace All" or use the keyboard shortcut (Ex. ctrl+alt+enter).
This will replace each instance of any non-whitespace character in the selection with a space character.
If you don't want to preserve the types of whitespace characters (Ex. keeping tab characters as tab characters), just put "." in the find input field instead of "[^\s]".
You can use the extension Regex Text Generator
Add this to your settings.json
"regexTextGen.predefined": {
"Replace with spaces": {
"originalTextRegex": "(?g)(.)",
"generatorRegex": " {S}"
}
}
Select the text you want to replace, multi cursor is supported.
Execute command: Generate text based on Regular Expression (regex)
Select the predefined: Replace with spaces
Press Enter 2 times.
You are able to preview the replacement and Escape if needed when you are allowed to edit the generator expression.
You can create a key binding for this if you need to do it a lot. See the extension page for an example.

Visual Studio Code multiline select until a character

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":

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