VSCode jump to the middle of current line - visual-studio-code

There are many keyboard shortcuts and edit options in Visual Studio Code, I can move cursor: by character, by word, to beginning/end of line. But, if I'm editing a long line of text, how to faster jump to the middle of this line ?
Found similar question here, but for vim users - Visually select to the middle of line
Probably, it's possible to implement this option (jump to middle) with Vim extension. If so, then how to do it ?
Is there a way to make such behaviour natively without any extensions ?

Actually, this is built-in to vscode, using the cursorMove command. Set up this keybinding in your keybindings.json:
{
"key": "alt+c",
"command": "cursorMove",
"args": {
"to": "wrappedLineColumnCenter",
"select": true // default is false
}
}

You can use the extension Select By v1.0.0.
You can create a keybinding that calculates the new cursor position
{
"key": "ctrl+i ctrl+m", // or any other key binding
"when": "editorTextFocus",
"command": "moveby.calculation",
"args": {
"charNrEx": "currentLine.length / 2"
}
}

Related

How to move up and down in quick fix suggestions in visual studio code without using arrow keys?

Whenever there is some missing or needs correction in code, visual studio code shows a yellow bulb to show some quick fixes suggestions.
Is there any way or shortcut by which we can navigate up and down suggestion options shown as in screenshot above without using the arrow keys?
In vscode v1.71 there are three new commands you can use for navigating in the QuickFix menu to go to previous or next source code actions.
selectNextCodeAction // to focus the nextcode action
selectPrevCodeAction // to focus the previous code action
acceptSelectedCodeAction // to run the focused/selected code action
You can see the default keybindings below that have been removed. Note the - before the command name in two of the keybindings, that removes those keybindings.
You can make these keybindings (in your keybindings.json):
{
"key": "alt+u", // whatever you want here
"command": "selectPrevCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "up", // default removed
"command": "-selectPrevCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "alt+d", // whatever you want here
"command": "selectNextCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "down", // default removed
"command": "-selectNextCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
}
--------
There is also an extension presented as a fix:
Keyboard QuickFix, but it shouldn't be necessary anymore.
Go to "Keyboard shortcuts" and search for selectNextSuggestion.
You can rebind this command to any shortcut you like.
Same goes for selectPrevSuggestion.
I use cmd + j for up and cmd cmd + k for down ^^

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.

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:

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.

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.