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

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.

Related

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.

vscode auto parentheses not working some cases, can i write some words by a keyboard shortcut?

It's hard for me to press the shift+9 and then shift+0 when I need parentheses.
I want when I type print and press tab or enter then be converted to print()
or when I am defining or calling a function.
But this doesn't happen. I tried with python extension, pylance and tabnine (free version).
Maybe you would suggest a user snippet but they are not very pleasant in this way.
I mean if I create a snippet with pa key and () body,
I must first write print pa to be converted to print () and then I must remove space in middle.
It would be great if there was a way to write a few words just by a shortcut.
For example, control+enter become to ()
P.S. English is not my first language, sorry if there are grammatical mistakes.
{
"key": "ctrl+enter", // whatever keybinding you like
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "()"
}
},
That is a keybinding fro your keybindings.json file.
This does the same:
{
"key": "ctrl+enter",
"command": "type",
"when": "editorTextFocus",
"args": {
"text": "()"
}
},
also a keybinding for the keybindings.json file.

VSC keybindings

I have tried to implement this feature for a long time. I have not been able to find answers online. I would like to use "Tab" key to do two things.
I want to indent if cursor is at the beginning of a line, or
jump to end of line if cursor is between characters/strings.
[
{
"key": "ctrl+tab",
"command": "tab",
"when": "editorFocus && inputFocus && !editorHasSelection"
},
{
"key": "tab",
"command": "cursorEnd",
"when": "textInputFocus"
}
]
These are similar features that are in Eclipse and Intellij IDE
You should be able to achieve this by modifying the extension TabOut, which indents except when the cursor is next to a bracket or brace, in which case it tabs through.
If you instead of a brace, you make it behave that way next to any character except newlines, it should jump to end of line.

vscode insert date time each new line?

I would like to (ab)use vscode as a tool for some specific live-logging.
For this purpose I would like to have it insert the current date, time at the beginning of each line. I did look here: How to insert current date time in vscode? but it only goes half-way there. I have only recently moved from Atom to VScode and am a bit daunted by trying to do this from first principles - any pointers would be most welcome!
As a bonus: if this function could be created so that it only takes place in files which have been saved with a specific file extension, that would be even more awesome! Otherwise I need to turn on/off this feature every time I use VScode for something else...
Using a macro extension like multi-command put this into your settings:
"multiCommand.commands": [
{
"command": "multiCommand.insertTimeonNewline",
"sequence": [
"editor.action.insertLineAfter",
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "$CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND "
}
},
]
}
]
And this into keybindings.json:
{
"key": "enter",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.insertTimeonNewline" },
"when": "editorTextFocus && resourceExtname =~ /\\.php/"
},
Change that extension at the end of the when clause to whatever extension you need to use.
Now Eenter will enter a new line and put the time at the beginning of it. You should modify the time variables if you want year, month, etc.

How do I navigate to the next squiggly in the current file or project?

Often when I make bigger refactorings in my TypeScript codebase, I'd like to be able to navigate to the next underlined TS compiler error to fix it, then move on further etc. If I use "go to next problem" that always triggers a relatively big red popup message around that line. I would prefer for the cursor to simply jump to that position, I don't require any extra navigation etc. Is that possible?
If you don't get a simpler answer I think you may need a macro to do this so two commands are triggered. Using a macro extension like multi-command, put this in your settings:
{
"command": "multiCommand.nextError",
"sequence": [
"editor.action.marker.nextInFiles",
"closeMarkersNavigation",
]
},
and then in keybindings.json:
{
"key": "F8",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.nextError" },
"when": "editorFocus && !editorReadonly"
},
{
"key": "f8",
"command": "-editor.action.marker.nextInFiles",
"when": "editorFocus && !editorReadonly"
}
I get a quick flash when the next marker closes so this is not optimal but may be the best you can do.