VS Code - Key Bindings - my own Block comment - visual-studio-code

I am using VS Code Version: 1.40.0.
for quicken up my development I would need to set my own keybinding for block comment when I am in .phtml file.
I managed to get into keybindings.json, put this inside:
{
"key": "shift+alt+q",
"command": "editor.action.blockComment",
"blockComment": [ "{*<!--", "-->*}" ],
"when": "editorTextFocus && !editorReadonly && resourceExtname == .phtml"
}
I got the part
"blockComment": [ "{*<!--", "-->*}" ],
from here How to customize comment block characters in visual studio code?.
It might be a complete trash. I just tried. It doesn't work, of course.
Optimal solution:
Even better would be, if the default key parameter would stay the same (shift+alt+a) for toggle block comment and in .phtml files i would get my desired result ("{*<!-- -->*}").
If I think about it, there is default block comments for .css, .html etc, so there must be a way to put my condition somewhere, rigth?
I would be very glad for any help. Thanks in advance

As far as I know the best would be to write your own Language Extension plugin for .phtml files and set the desired comment pairs in its configuration.
Not a real solution but until then, here's my supper ugly workaround (use at your own risk):
Put this in your keybindings.json
{
"key": "ctrl+numpad_divide",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/^(\\s*)({\\*<!-- (.*) -->\\*})?(.*)/$1${3:-{*<!-- }$4${4:+ -->*\\}}/s}"
},
"when": "editorTextFocus && editorHasSelection && !editorReadonly && resourceExtname =~ /phtml?$/"
}
This way ctrl + / can be used for comment/uncomment (toggle) the selected code as you described. Of course you can set the key binding to something else like alt + / to keep the default block comment behaviour.
Tested with Visual Studio Code v1.50.1

Related

In VS Code, is it possible to execute keybinds based on the context of the line, or characters

For example, if I'm writing something like this:
vector<int> v(10);
v[2<cursor is here>]
Is there some kind of when clause to add to custom keybinds, i.e., something along these lines:
{
"key": "tab",
"command": "cursorRight",
"when": "textInputFocus && cursorNextChar == ']'"
}
And if nothing like that is built-in, what's a straightforward way to go about creating an extension that provides keybind extention customizability that executes commands based on the context of the line/characters around the cursor?
I tried using !atEndofWord but that breaks a lot of things like autocomplete and having tabs work as expected when not inside brackets.
The extension Extra Context (v0.4.0) fills the context variable extraContext:editorCursorNextChar with the character after the active position (cursor) of the first selection. The context variable is the empty string when the position is at the end of a line.
For your example set this key binding:
{
"key": "tab",
"command": "cursorRight",
"when": "textInputFocus && extraContext:editorCursorNextChar == ']'"
}

Copying a line when selection is empty does not work

When I try to Ctrl+C to copy the entire line that my cursor is on does not work, it simply doesn't change the current item in my clipboard. As soon as I make a selection, it manages to copy. I haven't changed much of the configurations, I only tried some stuff and my keybindings.json looks like this:
{
"key": "ctrl+numpad_divide",
"command": "macros.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+c",
"command": "editor.action.clipboardCopyAction"
},
{
"key": "ctrl+insert",
"command": "-editor.action.clipboardCopyAction"
}
Also worth mentioning is that I have tried with all the extensions disabled and the user-related keybindings.json and settings.json lines commented out. It just refuses to copy the line.
I think there could be a workaround by using the macros extension to create a macro to select the entire line and copy, then deselect it only when the selection is empty; however, I believe there should be a native fix for this as it is a trivial problem.

Keyboard shortcut in VSCode for Markdown links?

from other text editors I'm used to adding Markdown links by
selecting the word I want to be linked,
pressing cmd-K on my Mac's / iPad Pro's keyboard, which puts square brackets around the marked word, appends a pair of normal parenthesis () and places the cursor right in beetween those two parenthesis so that I can
just paste the URL I have in my clipboard into the right place by pressing cmd-V.
So, select -> cmd-K -> cmd-V is a nice and short sequence for adding links in a Markdown document and cmd-K has become some kind of pseudo standard for adding links in several writing apps.
However, in VSCode that's not possible. But I'd love to make it possible. Any ideas? cmd-K is (hard-wired?) bound to listen for a next key press.
But it doesn't have to be cmd-K. I can learn another keystroke. But I need to be able to put additional text (square brackets and parenthesis) into the text and move the cursor to the right position. How's that done?
Thanks so much!
This extension Markdown All In One looks like it does what you want in one step.
Paste link on selected text
Just select your link and hit Ctrl+V and it creates the link and inserts the clipboard link.
If for some reason you don't want to use this extension, it would be pretty easy to create a snippet to do what you want.
Adding another answer that doesn't use the extension Markdown All In One I mentioned in the other answer and because a couple of commenters requested a different way. #MarcoLackovic
First Method: keybinding in keybindings.json, then manually paste
{
"key": "alt+w", // use whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
},
"when": "editorHasSelection && editorLangId == markdown "
}
Select the link text and, trigger your keybinding - the cursor will be placed where you want it and paste.
Second Method: use a macro to insert the snippet and paste in one step
You will need a macro extension like multi-command to run multiple commands in series. Then this keybinding:
{
"key": "alt+w",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
}
},
"editor.action.clipboardPasteAction"
]
},
"when": "editorHasSelection && editorLangId == markdown "
}
Demo of second method:

Auto-add quotes around new lines in VS Code like Netbeans does

Just about completed my transition from Netbeans to VS Code, and there's one thing missing from VS Code that I miss dearly from Netbeans that essentially handles quotes automatically when you press enter from within a string.
These pictures should show what I mean, with this first picture being a lengthy string inside of Netbeans.
And this one is after I press enter somewhere in the middle of the string without pressing any other keys
You can see that it puts a quote where the cursor was, adds a newline, adds indentation, adds a dot (the PHP concat operator), and then another quote, which is such a fantastic feature.
Two things; what is this called, and how do I get this behavior in VS Code?
Pretty easy to do with a macro. Install the macrosRE extension.
In your settings.json:
"macros": {
"netbeans": [
{
"command": "type",
"args": {
"text": "\"\n\t\t. \""
}
}
]
},
and set up some keybinding for it in keybindings.json:
{
"key": "ctrl+alt+n",
"command": "macros.netbeans"
},
It would really interesting if there was a "when" condition to detect if within a string (and within a php file) ...and then bind to Enter. But I doubt there is such a "when" clause.
[EDIT]:
I should have remembered that in your case the 'macro' is so simple that you don't need to use the macro functionality. Try simply this in your keybindings.json:
{
"key": "ctrl+alt+n",
"command": "editor.action.insertSnippet",
//"when": "editorTextFocus && editorLangId == php",
//"when": "editorTextFocus && resourceLangId == php"
"args": {
"snippet": "\"\n\t\t. \""
}
}
It just inserts a snippet which is right there in the args. You may or may not want the 'php' limitation. You can also use the method below if you want to insert a snippet that actually lives in a snippets file:
{
"key": "cmd+k 1",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"langId": "csharp",
"name": "myFavSnippet"
}
}
From vscode doc: assigning a keybinding to a snippet.
But you will eventually want to chain commands together which the macros extension allows you to do.

Visual Studio Code - Indent single line with tabulator-key

I would like to know if its possible to indent a single line with the tab-key without deleting the marked text.
In the first part of the GIF you see Visual Studio Code and in the second part Atom. Atom shows the desired behaviour.
Thus far it is possible to indent multiple lines this way in VS Code, it also works with backtab, but not with tab and a single line.
Is this a bug or normal behavior??
My Setup:
Visual Studio Code: Version 1.25.1 (MacOS 10.13.6 High Sierra)
Visual Studio Code: Version 1.25.1 (Ubuntu 18.04 LTS)
You could use this default keybinding:
{
"key": "ctrl+]",
"command": "editor.action.indentLines",
"when": "editorTextFocus && !editorReadonly"
}
to tab single or multilines. If you want that bound to tab you could modify it to:
{
"key": "tab",
"command": "editor.action.indentLines",
"when": "editorHasSelection && editorTextFocus && !editorReadonly"
}
I added the editorHasSelection clause so it operates only when something is selected on your line, but then you would lose the normal simple tab behavior (that you don't like).
From my understanding, this is the expected behavior. To indent a single line, you'd need to either:
place cursor at beginning of the line and then tab
select the entire line (Mac: Command+i, Windows/Linux: Ctrl+i) and then tab
use the indent line command, which can be done with the words selected as shown in your GIF (Mac: Command+], Windows/Linux: Ctrl+])
There may be an extension available that gives you your desired behavior, though.
Just adding another flavor here:
In case you want tab to work like shift-tab (where you don't have to highlight anything), AND if you're using tab as the key to accept autocomplete suggestions, use this setting:
{
"key": "tab",
"command": "editor.action.indentLines",
"when": "!suggestWidgetVisible && editorTextFocus && !editorReadonly"
}