select next occurence of a word when using multiple cursors - visual-studio-code

From the bellow screen shot you can see that I have placed multi-cursor one multiple lines.
I want the next occurrence of " to be selected on each line for each cursor. I know I can use ctrld for selecting next occurrence. But it only works for single cursor. What is the solution for multi-cursor?

with the extension Select By v0.11.0 and the command moveby.regex you can move multiple cursors based on a regular expression.
The regular expression can be fixed in the keybinding or asked from the user.
For fixed regex use a key binding like:
{
"key": "ctrl+f6", // or any other key combo
"when": "editorTextFocus",
"command": "moveby.regex",
"args": {
"regex": "\"",
"properties": ["next", "start"]
}
}
To ask for a regex use a key binding like:
{
"key": "ctrl+shift+f6", // or any other key combo
"when": "editorTextFocus",
"command": "moveby.regex",
"args": {
"ask": true,
"properties": ["next", "start"]
}
}
In the Inputbox enter the regular expression in a non-escaped form. In a JSON files you need to escape the " and \ characters.
In a later version the command selectby.regex will also support multi cursor.

Related

how to put increased number for multiline selection in visual studio code

I want to add increased number for multi selected caret in visual studio code.
now, When I type it write same words.
But I would like to add increased number by some shortkey so that I don't need to update each one manually.
Preferred result should be like this.
I want to know if this is possible in vs code.
Thanks
You do not need an extension for your use case, although that may make it easier. Here is how to do it without an extension.
Find: (?<=index:\s*)\d+ : this selects only the digits following index: .
Alt+Enter will select all those digits.
Now you can run a simple snippet to replace those digits with an increasing number that could be 0-based or 1-based. Make this keybinding to insert the snippet (in your keybindings.json):
{
"key": "alt+m", // whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
"snippet": "$CURSOR_NUMBER" // this will increment and is 1-based
}
}
Trigger the above keybinding. Demo:
Here is an extension approach, using an extension I wrote, Find and Transform, that makes this easy. Make this keybinding:
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(?<=index:\\s*)\\d+", // same find regex
"replace": "${matchNumber}", // this variable will increase, 1-based
"isRegex": true
}
}
That combines the find and replace in one step.
Here is another method so you do not need to hardcode the starting point.
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"preCommands": [
"editor.action.addSelectionToNextFindMatch",
"editor.action.clipboardCopyAction"
],
"find": "(?<=index:\\s*)\\d+",
"replace": [
"$${",
// whatever math you want to do here
"return Number(${CLIPBOARD}) + ${matchIndex};",
"}$$",
],
"isRegex": true,
"postCommands": "cancelSelection"
}
}
Put the cursor next to or select the number you want as the starting point. The number could be anywhere in the document actually.
You can use the extension Regex Text Generator
Define the following key binding
{
"key": "ctrl+shift+f9", // or any other key combo
"when": "editorTextFocus",
"command": "regexTextGen.generateText",
"args": {
"generatorRegex" : "{{=i+1}}"
}
}
place the multi cursors after index:
press the key combo
accept or modify the inputs
look at the preview, press Enter if you like it, Esc to abort
You can do it with Increment Selection or Text Pastry

VS Code - can I bind a character sequence to a function key?

I use VS Code with the Lean language, which supports Unicode characters. The Unicode characters are entered through sequences of ASCII characters. For example, to enter a universal quantifier you enter the five-character sequence \ a l l TAB. Is there any way in VS Code to bind that sequence to a function key so that I can say hit F1 and this is interpreted as entering the ASCII sequence?
I needed to bind a key in VSCode to a sequence of characters. Here's how to do that in VS Code 1.67.2:
Press ctrl+alt+P / cmd+alt+P to open the command palette.
Use the command "Preferences: Open Keyboard Shortcuts (JSON)"
Press the "Define Keybinding" button in the bottom right, or press cmd+K cmd+K / ctrl+K ctrl+K, or just type it in.
The following works for your case:
{
"key": "F1",
"command": "type",
"args": { "text": "\\all\t" },
"when": "editorTextFocus && !editorReadOnly"
},
and for my case
{
"key": "cmd+.",
"command": "type",
"args": { "text": "->" },
"when": "editorTextFocus && !editorReadOnly"
},
The binding will work as soon as you save the file.

VSCode Terminal command names for Up/Down Arrow hotkeys? [duplicate]

My current custom keybindings
{
"key": "alt+b",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "\u0017"
}
},
{
"key": "alt+j",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001bb" }
},
{
"key": "alt+l",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001bf" }
},
{
"key": "alt+n",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "\u001bd"
}
These work well with 1.45 update (integrated terminal update)
Reference - but I need more Information
I want to use 'ijkl' keys like 'wasd' gaming standard like my custom project intuiter
So My Idea is
Can we send sequence to terminal to show Previous Command/Next command (like when we click Up/Down Arrow)
moveToLineStart/moveToLineEnd sequence is removed with this update(sorry I can't find or make sequence) can we get this effect by sendSequence?
"Can we sendSequence to terminal to show Previous Command/Next Command (like when we click Up/Down Arrow)"
{
"key": "alt+x", // or whatever you choose
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u001b[A\n"
}
// 1 up arrow or previous command, works in cmd, git bash and powershell
// the \n at the end makes it execute the previous command immediately,
// leave it off if you don't want to do that
// \u000d, a carriage return, is equivalent to \n if you prefer to use that, so
// \u001b[A\u000d does the same thing as \u001b[A\n
> /** Carriage Return (Caret = ^M, C = \r) */
> export const CR = '\x0d';
{
"key": "alt+y", // or whatever you choose
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u001b[B\n"
}
// 1 down arrow
// see comment above about immediate command execution
For these arrow commands, see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
Functions using CSI , ordered by the final character(s)
CSI Ps A Cursor Up Ps Times (default = 1) (CUU).
CSI Ps B Cursor Down Ps Times (default = 1) (CUD).
This part \u001b[ is the escape sequence or CSI referred to in the document. Follow that with an A for cursur up (up arrow) so \u001b[A or follow the escape sequence with a B for a down arrow, so \u001b[B.
[Theoretically, you should be able to do \u001b[2A for 2 (the Ps part referred to above) up arrows at once but that never seems to work in vscode for me in vscode.]
{
"key": "ctrl+e",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0005" }, // move cursor to end of line, bash at least
"when": "terminalFocus"
},
{
"key": "ctrl+a",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0001" }, // move cursor to start of line, bash at least
"when": "terminalFocus"
},
I used the usual keybindings - at least in bash - for go to the start or end of the command line. See, e.g., http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/
From this document https://github.com/xtermjs/xterm.js/blob/0e45909c7e79c83452493d2cd46d99c0a0bb585f/src/common/data/EscapeSequences.ts
we see that Ctrl+A is :
/** Start of Heading (Caret = ^A) */ [that's Ctrl+A]
export const SOH = '\x01';
so I used \u0001 the unicode sequence to replace that listed \x01 which won't work with the sendSequence command.
Likewise, to send a Ctrl+E to the terminal for the go to the end of the command line, we see that Ctrl+E is :
/** Enquiry (Caret = ^E) */ [that's Ctrl+E]
export const ENQ = '\x05';
or unicode \u0005.
Now, your terminal shell might use something other than Ctrl+A and Ctrl+E for go to start/end of the command line. If my keybindings don't work for you, find out what your shell uses for go to start/end and see if they are in https://github.com/xtermjs/xterm.js/blob/0e45909c7e79c83452493d2cd46d99c0a0bb585f/src/common/data/EscapeSequences.ts

Need VSCode sendSequence keybindings for previous command, next command, move to start of line, move to end of line terminal commands

My current custom keybindings
{
"key": "alt+b",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "\u0017"
}
},
{
"key": "alt+j",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001bb" }
},
{
"key": "alt+l",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001bf" }
},
{
"key": "alt+n",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "\u001bd"
}
These work well with 1.45 update (integrated terminal update)
Reference - but I need more Information
I want to use 'ijkl' keys like 'wasd' gaming standard like my custom project intuiter
So My Idea is
Can we send sequence to terminal to show Previous Command/Next command (like when we click Up/Down Arrow)
moveToLineStart/moveToLineEnd sequence is removed with this update(sorry I can't find or make sequence) can we get this effect by sendSequence?
"Can we sendSequence to terminal to show Previous Command/Next Command (like when we click Up/Down Arrow)"
{
"key": "alt+x", // or whatever you choose
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u001b[A\n"
}
// 1 up arrow or previous command, works in cmd, git bash and powershell
// the \n at the end makes it execute the previous command immediately,
// leave it off if you don't want to do that
// \u000d, a carriage return, is equivalent to \n if you prefer to use that, so
// \u001b[A\u000d does the same thing as \u001b[A\n
> /** Carriage Return (Caret = ^M, C = \r) */
> export const CR = '\x0d';
{
"key": "alt+y", // or whatever you choose
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u001b[B\n"
}
// 1 down arrow
// see comment above about immediate command execution
For these arrow commands, see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
Functions using CSI , ordered by the final character(s)
CSI Ps A Cursor Up Ps Times (default = 1) (CUU).
CSI Ps B Cursor Down Ps Times (default = 1) (CUD).
This part \u001b[ is the escape sequence or CSI referred to in the document. Follow that with an A for cursur up (up arrow) so \u001b[A or follow the escape sequence with a B for a down arrow, so \u001b[B.
[Theoretically, you should be able to do \u001b[2A for 2 (the Ps part referred to above) up arrows at once but that never seems to work in vscode for me in vscode.]
{
"key": "ctrl+e",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0005" }, // move cursor to end of line, bash at least
"when": "terminalFocus"
},
{
"key": "ctrl+a",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0001" }, // move cursor to start of line, bash at least
"when": "terminalFocus"
},
I used the usual keybindings - at least in bash - for go to the start or end of the command line. See, e.g., http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/
From this document https://github.com/xtermjs/xterm.js/blob/0e45909c7e79c83452493d2cd46d99c0a0bb585f/src/common/data/EscapeSequences.ts
we see that Ctrl+A is :
/** Start of Heading (Caret = ^A) */ [that's Ctrl+A]
export const SOH = '\x01';
so I used \u0001 the unicode sequence to replace that listed \x01 which won't work with the sendSequence command.
Likewise, to send a Ctrl+E to the terminal for the go to the end of the command line, we see that Ctrl+E is :
/** Enquiry (Caret = ^E) */ [that's Ctrl+E]
export const ENQ = '\x05';
or unicode \u0005.
Now, your terminal shell might use something other than Ctrl+A and Ctrl+E for go to start/end of the command line. If my keybindings don't work for you, find out what your shell uses for go to start/end and see if they are in https://github.com/xtermjs/xterm.js/blob/0e45909c7e79c83452493d2cd46d99c0a0bb585f/src/common/data/EscapeSequences.ts

Using TM_SELECTED_TEXT in my custom snippets

With the November 2016 (version 1.8) release of VSCode Snippet Variables are now supported, specifically TM_SELECTED_TEXT.
This makes me happy as I have used these heavily in both Sublime Text and TextMate.
I can't figure out how to get it to work in VSCode. I've created the snippet they use as an example:
"in quotes": {
"prefix": "inq",
"body": "'${TM_SELECTED_TEXT:${1:type_here}}'"
}
I then enter some text, highlight it and that's where things start to break.
The idea is highlight some text, run the snippet and then ${TM_SELECTED_TEXT:${1:type_here}} is replaced with the highlighted text. The problem I'm having is that to run the snippet you need to type the prefix value (in this case inq) to run the snippet which over-writes your highlighted text which messes everything up.
In Sublime/Textmate I launched the snippet from a keyboard combination which left my text highlighted.
Is there a way, in VSCode, to either make this work as is or launch the snippet from a key combination like was available in Sublime?
Coming in 1.49 (it is in the Insiders' Build as of this edit) your example will finally work as you expected. See merged pull request.
Vscode will now "remember" your selected text if any, and when you type your snippet prefix, insert it into the TM_SELECTED_TEXT variable even though you seemingly over-typed that selected text.
As of v1.20 this has become easier as a new variable $CLIPBOARD has been added, see new snippet variables. So there is no need to assign and run a shortcut - but you must copy the selection to clipboard CTRL-C.
Your example could now be:
"in quotes": {
"prefix": "inq",
"body": "'$CLIPBOARD:${1:type_here}'"
}
Note: $CLIPBOARD works. There's no need for the extra curly brackets {$CLIPBOARD}.
With the word highlighted, press F1 and run the command "Insert Snippet", then select your snippet on the list.
Also you can edit your keybindings by going to File>Preferences>Keyboard Shortcuts and add some shortcut to the "editor.action.showSnippets" command, like this:
{
"key": "ctrl+alt+s",
"command": "editor.action.showSnippets",
"when": "editorTextFocus"
}
https://github.com/Microsoft/vscode/issues/17780
as per this thread you can assign exact snippet to keybinding by providing args.
keybinding example for bootstrap media queries
{
"key": "ctrl+alt+b",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"name": "bsup"
}
},
{
"key": "ctrl+alt+shift+b",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"name": "bsup_copy"
}
},
snippet example
"bsup": {
"prefix": "bsup",
"body": [
"#include media-breakpoint-up(md){",
"\t${TM_SELECTED_TEXT}",
"}"
],
"description": "Bootstrap media up"
},
"bsup_copy": {
"prefix": "bsup_copy",
"body": [
"${1:${TM_SELECTED_TEXT}}",
"#include media-breakpoint-up(md){",
"\t${2:${TM_SELECTED_TEXT}}",
"}"
],
"description": "Bootstrap media up + copy selected text"
},
UPD: moreover - you can define snippet directly in keybindings.json which seems to be even more convenient for me in some cases
{
"key": "cmd+shift+c",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.log('${TM_SELECTED_TEXT}', $TM_SELECTED_TEXT$1);"
}
}
using documentation from https://code.visualstudio.com/docs/editor/userdefinedsnippets i was able to customize snippets, i am using 'surround with' extension and can put my own snippet into settings.json as follows:
"html_h3-name": {
"label": "h3",
"description": "wrap by h3 with <a name=''>, top",
"snippet": "<h3><a name=\"${TM_SELECTED_TEXT/[\\s]/-/g}\"></a>$TM_SELECTED_TEXT\n\t<a class=\"small\" href=\"#top\">top</a>\n</h3>"
},
which takes highlighted code in VSCode and creates h3 header from it with a name link:
it converts 'aaa bbb ccc' to
<h3><a name="aaa-bbb-ccc"></a>aaa bbb ccc
<a class="small" href="#top">top</a>
</h3>