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"]
}
Related
I am trying to create a snippet that surrounds the selected text with try..except block. This is what I have in keybindings.json file:
{
"key": "ctrl+p'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "try:\r\n\t${TM_SELECTED_TEXT}\r\nexcept BaseException as ex:"
}
}
This works for most part except that if I select the entire line of indented code, it inserts try at the beginning of the line. I want it to behave like Command+/ which adds # right before where the text starts.
How do I make my snippet behave like that?
You have to insert a possible whitespace in front of each line and remove the whitespace on the middle line:
{
"key": "ctrl+p'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}try:\r\n${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}\t${TM_SELECTED_TEXT/^[ \\t]*(.*)$/$1/}\r\n${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}except BaseException as ex:"
}
}
Dang, it took a while but here is something pretty simple that I think works.
First, make this snippet (in some snippets file):
"try except": {
// "scope": "python",
// "prefix": "tryWrap", // if you want it
"body": [
"${1:${TM_SELECTED_TEXT/^([ \\t]*)[\\s\\S]*$/$1/}}try:",
"${TM_SELECTED_TEXT/^(.*?)$(\\r?\\n)?/\t$1$2/gm}", // note the 'g' flag !!!
"$1except BaseException as ex:"
]
}
and then this keybinding (in your keybindings.json):
{
"key": "alt+q",
"command": "editor.action.insertSnippet",
"args": {
"name": "try except",
}
}
That middle line of the snippet:
"${TM_SELECTED_TEXT/^(.*?)$(\\r?\\n)?/\t$1$2/gm}"
will actually run once for each line of your selection, because the match is from ^ to the end of the same line and because of the global g flag. So it'll keep running as long as it finds matches in your entire selection.
The leading whitespace is computed by ${1:${TM_SELECTED_TEXT/^([ \\t]*)[\\s\\S]*$/$1/}} which will be the first line of your selection. It isn't computed for each line (although it probably could be, it would just be unnecssarily messy). So don't select part of the leading white space of that first line of the selection - actually it seems to work fine as long as select whole tabs worth of that leading white space, just not an extra space. It is easy to do it right.
#rioV8's snippet works for me (for single lines only it seems) but it could be simplified a bit. I upvoted it.
Note that 3 parts of the snippet are identical: ${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}
So to simplify that resulting value (the whitespace before the selected text) can be stored in a value and reused. See this:
${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}} // wrapped in tabstop 1
Now you can just use $1 in any other place you want that same value.
"snippet": "${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}}try:\r\n$1\t${TM_SELECTED_TEXT/^[ \\t]*(.*)$/$1/}\r\n$1except BaseException as ex:"
See there are two $1's not part of a transform, like try:\r\n$1\t : that $1 will be your computed whitespace from ${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}}
Also, this part: ${TM_SELECTED_TEXT/^[ \\t]*(.*)$/$1/} can be simplified to
${TM_SELECTED_TEXT/^[ \\t]*//}
which matches the leading white space before the text and replaces that white space with nothing.
Result:
"snippet": "${1:${TM_SELECTED_TEXT/^([ \\t]*).*$/$1/}}try:\r\n$1\t${TM_SELECTED_TEXT/^[ \\t]*//}\r\n$1except BaseException as ex:"
This is just a little cleaner and less prone to typos. You just have to do one tab or escape at the end to finish.
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.
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.
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.
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>