vscode insert date time each new line? - date

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.

Related

Can VSCode be extended with functionality to call functions exposed by a COM object

Probably easiest to explain my use case. I'm looking for a text editor for transcription rather than coding. BUT the key feature I need is the ability to extend the text editor with small "macro" snippets that can call a COM object interface.
In this case it would call a COM interface supplied with the audio playback tool that has functions to get the current timestamp, change to a different time in playback, turn channels on/off, jump forward/back, etc.
Practical example:
I press a shortcut key in VSCode
That triggers a macro of some kind
This macro instantiates a COM object for my player (we'll call that "myplayer")
The macro calls "myplayer.GetCurrentTimeStamp" which returns a timestamp as a value.
I format the output to get the time in h:mm:ss am/pm format.
The timestamp is inserted into my text file.
Possible?
If you have an external program that will store the timestamp in a temporary file you can use the extension Command Variable (v1.12) to read (part of) the file content and insert it in the editor.
Use the extension multi-command to combine the 2 commands.
Define a task that runs the external command
Define a multi-command that calls the task and then extension.commandvariable.file.contentInEditor
Define a key binding that calls the multi-command
Add to .vscode/tasks.json
{
"label": "get Timestamp",
"type": "shell",
"command": "echo timestamp=2021-04-01 12:34 >/tmp/timequery.txt",
"problemMatcher": []
}
Add to .vscode/settings.json
"multiCommand.commands": [
{
"command": "multiCommand.insertTimestamp",
"interval": 500,
"sequence": [
{ "command": "workbench.action.tasks.runTask",
"args": "get Timestamp"
},
{ "command": "extension.commandvariable.file.contentInEditor",
"args": {
"fileName": "/tmp/timequery.txt",
"key": "timestamp",
"default": "Query failed"
}
}
]
}
]
Add to keybindings.json
{
"key": "ctrl+F1", // or any other key combo
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.insertTimestamp" },
"when": "editorTextFocus"
}

Move cursor to the beginning of the multiple selected lines visual studio code

How do you move your cursor to the beginning of multiple selected lines in VScode ? i know that shift+alt+i is to go the end of the line but couldn't find the reverse. Don't confuse my question with the start of the document.
You can create a keybinding that uses a macro extension to run multiple commands, like multi-command
{
"key": "home", // whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorLineStart" // goto start of line before any leading whitespace
// "cursorHome" // goto start of text on each line
]
},
"when": "editorTextFocus && editorHasSelection"
}
It works really nicely, with one multi-line selection or multiple selections:

VSCode: Open files NOT in a new tab, reuse current tab

I believe that this is not covered by the Preview feature. I simply want to open a file for editing via Quick Open (or any way?) and replace the contents of the active tab, closing the open file and replacing it with the new one.
This behavior is central to the way I edit. Currently, I'm always opening new tabs that I don't want. It's the only barrier left between Code and the way I've used Vim for 15 years. I imagine that this is scriptable, but would like to avoid going down that road. Please tell me I'm missing something.
(1) The drastic approach: search for these in your settings:
Workbench > Editor > Limit: Enabled enable this
Workbench > Editor > Limit: Value set to 1
Drastic, because it will limit you to only 1 editor tab, probably not what you want but it does reuse the active (and only tab) of course.
(2) The macro approach:
Using a macro extension like multi-command put this into your settings.json
"multiCommand.commands": [
{
"command": "multiCommand.openFileInActiveEditor",
"sequence": [
"workbench.action.closeActiveEditor",
"workbench.action.acceptSelectedQuickOpenItem",
"workbench.action.closeQuickOpen" // if you want to close the quickopen panel immediately
]
}
]
and in keybindings.json:
{
"key": "alt+0", // whatever you want
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.openFileInActiveEditor" },
"when": "inFilesPicker && inQuickOpen"
},
It appears that you cannot override the usual right keybinding from the quickOpen panel so I set it to alt+right instead but you can pick whatever you want.
#Mark's answer almost gets you there, but it doesn't work with new (one tab) panes. Here's a modified version of his settings.json edit that does.
Install the multi-command extension
Put this in settings.json
"multiCommand.commands": [
{
"command": "multiCommand.openFileInActiveEditor",
"sequence": [
"workbench.action.acceptSelectedQuickOpenItem",
"workbench.action.previousEditor",
"workbench.action.closeActiveEditor",
"workbench.action.closeQuickOpen"
]
}
]
Put this in keybindings.json and replace the dummy value for the key key with your desired key combination
{
"key": "some+key+combination",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.openFileInActiveEditor" },
"when": "inFilesPicker && inQuickOpen"
},

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.

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.