Copying a line when selection is empty does not work - visual-studio-code

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.

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 == ']'"
}

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: 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.