I already found that the "command": "editor.action.duplicateSelection"
will duplicate the selection right next to it.
I want to duplicate the selected text to a new line. The selection may not be the entire line.
If you are talking about a selection that is less than the entire line, there is no built-in way to duplicate selected text to the next line. It can be done with a macro extension which enables you to run multiple commands at once.
Using the macro extension multi-command try this keybinding (in your keybindings.json):
{
"key": "alt+i", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
"editor.action.clipboardPasteAction",
{ // to add text after the selection
"command": "type", // you could also put this before the paste command
"args": { "text": " myText here after paste " }
}
]
}
}
That will copy the selected text, insert a blank line after it and paste that text there. Demo:
Demo with adding static text to the duplicated text:
Click File > Preferences > Keyboard Shortcuts:
Look for the Copy Line Down keyboard shortcut.
Related
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
Whenever I double click a word in vscode it gets highlighted and selected, as it should. But something else happens sometimes: the whole sentence gets highlighted. Is there a way for me to easily select the whole highlighted sentence?
In the picture I double clicked the word "test", as you can see it is highlighted in a different color, but the whole sentence is also highlighted. Only the word "test" is selected.
(this is in a javascript file but it probably does the same in other formats)
You can use the extension Select By
"selectby.regexes": {
"SelectSentence": {
"backward": "'",
"forward": "'",
"forwardInclude": false,
"backwardInclude": false
}
}
You can use the command palette command: Select text range based on regex and select SelectSentence from the list
Or setup a keybinding
{
"key": "ctrl+shift+alt+f9", // or any other key combo
"when": "editorTextFocus",
"command": "selectby.regex",
"args": ["SelectSentence"]
}
If I comment selected text, only that text is commented in Sublime Text but in VSCode the entire line is commented not just the selected text.
How can I solve this?
You can comment out part of a line by selecting the text and clicking ALT+SHIFT+A - Toggle Block comment option.
With a macro extension macro-commander you can do what I think you want - toggle block comment if you select something less than the entire line, otherwise if you select nothing on the line or the entire line toggle line comment it.
With this in your settings.json:
"macros": {
"commentSelection" : [
{
"javascript": [
"const editor = vscode.window.activeTextEditor;",
"const document = editor.document;",
"const selection = editor.selection;",
// Get the trimmed "word(s)" of the selection
"const word = document.getText(selection).trim();",
// Get the full line of text on the line of the selection
"let lineNumber = selection.start.line;",
"const line = document.lineAt(lineNumber).text.trim();",
// `!word` means the cursor is on the line but nothing selected
"let selectionEqualsEntireLine = !word || (line === word);",
"if (selectionEqualsEntireLine) vscode.commands.executeCommand('editor.action.commentLine');",
"else vscode.commands.executeCommand('editor.action.blockComment');",
]
}
]
and your keybinding to trigger it:
{
"key": "ctrl+;", // use whatever keybinding you wish
"command": "macros.commentSelection"
}
Alternatively, and much simpler, try these keybindings and no macro at all:
{
"key": "ctrl+;",
"command": "editor.action.blockComment",
"when": "editorTextFocus && editorHasSelection"
},
{
"key": "ctrl+;",
"command": "editor.action.commentLine",
"when": "editorTextFocus && !editorHasSelection"
},
The only difference from the macro is if you select all the text - with the macro you would get a line comment, with these keybindings you will get a block comment. I an not sure which version you prefer.
I know there is a shortcut for comment and uncomment code block (SHIFT + ALT + A), but is there a way to quickly select (or even remove without select) block comment without using mouse or keyboard to select and press the delete/backspace button? For example:
/*
This is a large block of code with at least 50 lines of code!
:
:
*/
Is there a keyboard shortcut where I can place my cursor anywhere in the block comment and remove it in just a few keystrokes? Thanks!
You can set a macro to do this pretty easily.
First, use the excellent Select By extension (#rioV8) to select the text between and including the block comment markers /* and */. Put his into your settings:
"selectby.regexes": {
"BlockCommentSelect": {
"backward": "\/\\*",
"forward": "\\*\/",
"forwardInclude": true,
"backwardInclude": true,
"showSelection": true
}
},
You can use that with a keybinding like:
{
"key": "alt+s", // whatever keybinding you wish
"command": "selectby.regex",
"args": ["BlockCommentSelect"],
"when": "editorTextFocus"
},
You could stop here and use your keybinding to select the text and then Shift+Alt+A to toggle off the block comment.
Or you could add the selectby.regex1 to a macro and do it the selection and toggling off in one step. Here using the macro extension multi-command put this into your settings as well as the above selectby.regexes setting:
"multiCommand.commands": [
{
"command": "multiCommand.BlockCommentOff",
"sequence": [
{
"command": "selectby.regex",
"args": ["BlockCommentSelect"]
},
"editor.action.blockComment"
]
},
]
and then a keybinding to trigger that macro (in your keybindings.json):
{
"key": "shift+alt+A", // trigger the macro with whatever keybinding if you wish
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.BlockCommentOff" },
"when": "editorTextFocus && !editorHasSelection"
},
Here I used Shift+Alt+A to trigger the macro. And I used the when clause !editorHasSelection because if you have a selection maybe you want to block comment only that selection (inside another block comment!!).
Demos: (1) Just the first method where selectby selects your text and you manually toggle it off, and then (2) using the macro version to do it in one step.
How do I search for the selected text in the current file without having to copy / ctrl-f / paste?
For clarification: Ultraedit has this behaviour. When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.
Enable the editor.find.seedSearchStringFromSelection setting as seen below. This will cause highlighted texted to automatically be searched when you press ctrl+f.
Ultraedit-way search is my favorite and it's really convenient: single 'F3' key can handle all.
The drawback of Ctrl+D is: it cannot wrap around the search.
To be clear, the definition of Ultraedit-way search is:
When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.
Here is the absolutely 100%-compatible solution to Ultraedit-way search:
When text is selected, do nextSelectionMatchFindAction
When no text is selected, do nextMatchFindAction
Single F3 does both above.
Shift+F3 keeps its original: Find Previous
Therefore keybindings.json can add the following lines to disable original F3 and Ctrl+F3 functions, and add two new F3 functions when text is selected and not selected.
{
"key": "f3",
"command": "editor.action.nextSelectionMatchFindAction",
"when": "editorFocus && editorHasSelection"
},
{
"key": "ctrl+f3",
"command": "-editor.action.nextSelectionMatchFindAction",
"when": "editorFocus"
},
{
"key": "f3",
"command": "editor.action.nextMatchFindAction",
"when": "editorFocus && !editorHasSelection"
},
{
"key": "f3",
"command": "-editor.action.nextMatchFindAction",
"when": "editorFocus"
}
And one more thing to be addressed:
When F3 is pressed, the search dialog is appeared and every matched text is highlighted, you can press ESC to dismiss search dialog when search is done.
Update #2021/1/25
If anyone wants Shift+F3 to works as smart as F3, add the following line into keybindings.json:
{
"key": "shift+f3",
"command": "editor.action.previousSelectionMatchFindAction",
"when": "editorFocus && editorHasSelection"
},
{
"key": "shift+f3",
"command": "editor.action.previousMatchFindAction",
"when": "editorFocus && !editorHasSelection"
},
{
"key": "shift+f3",
"command": "-editor.action.previousMatchFindAction",
"when": "editorFocus"
},
I have found what I was looking for.
Ctrl+D triggers the action Add Selection To Next Find Match which does exactly what I wanted: perform an immediate search for the selected text, just like F3 in Ultraedit.