Keep VS Code cursor on screen - visual-studio-code

How can I keep the text cursor (caret) on the screen when scrolling in VS Code with Ctrl + up/down?
I've searched for scroll options, text options, extensions, etc. To no avail.

This is what I'm using in keybindings.json and it pretty much matches Visual Studio's Ctrl+Up/Down behaviour. (ie: basically SebastianDB's answer but for both keys and line up/down instead of page. Also, you don't need the macros extension, it works out of the box).
{
"key": "ctrl+up",
"command": "editorScroll",
"args": {
"to": "up",
"by": "line",
"revealCursor": true
},
"when": "editorTextFocus"
},
{
"key": "ctrl+down",
"command": "editorScroll",
"args": {
"to": "down",
"by": "line",
"revealCursor": true
},
"when": "editorTextFocus"
}

I haven't tried this myself but have you looked at this yet?
Install this:
https://marketplace.visualstudio.com/items?itemName=geddski.macros
then add a macro to ctrl+up/down with this sample and change the key from alt+pageup to what you want.
https://github.com/Microsoft/vscode/issues/22796
{
"key": "alt+pageup",
"command": "editorScroll",
"args": {
"to": "up",
"by": "page",
"revealCursor": true
},
"when": "editorTextFocus"
}
Hope it works, have a good one!

Maybe you are looking for something similar like scrolloff in vim?
Then add this to you settings.json:
"editor.cursorSurroundingLines": 9999,

Related

VS Code Keybinding for Line Break

I would like to add a key binding for inserting a line break in the editor (textInputFocus). Normally the Return key does this (VS Code calls the key Enter). Which command in VS Code can I use?
There is lineBreakInsert but this does not behave exactly the same as Enter normally does - it inserts a line break but keeps the cursor in the same position.
Is this what you are looking for:
{
"key": "ctrl+o",
"command": "type",
"args": {
"text": "\n"
},
"when": "textInputFocus"
}
Currently this is my workaround, but it's not a perfect solution:
I installed multi-command and added this binding (in keybindings.json):
{
"key": "ctrl+o",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"lineBreakInsert",
"cursorDown",
"cursorHome"
]
},
"when": "textInputFocus"
}

multiple key bindings in vscode (visual studio code) cursor right disabled after creation on new keybinding

Am new in VScode and i added a new shortcut to move the cursor right, as the right arrow key, forces me to move my hand, NOW THE RIGHT ARROW IS DISABLED and i can do other tasks such moving a full word right as the shortcut has completely replaced the right arrow key,
can someone tell me an easy shortcut to move the cursor to the right without the right arrow after each line of code, such as print(" hello world")cursor here
or tell me how to make both my custom shortcut sequence and the right arrow key work
also i noticed that the default keybinding (shift+alt+i) to move the cursor to the end of the line never worked.
thank you in advance
i found a similar post here: Visual Studio Code - multiple keyboard shortcuts?
and I've fixed moving right, but i can't do the same with the arrow up, no matter what keybinding i set,also would be nice to be able to add such moving shortcuts without having to affect the current right setting
**here is my current code on the keybiding.jsonfile - could anyone suggest a fix on this code without disrupting the current settings? **
{
"key": "shift+alt+i",
"command": "-editor.action.insertCursorAtEndOfEachLineSelected",
"when": "editorTextFocus"
},
{
"key": "shift+alt+r",
"command": "cursorRight",
"when": "textInputFocus"
},
{`enter code here`
"key": "Right",
"command": "cursorRight"
}
{
"key": "Left",
"command": "cursorLeft"
}
{
"key": "right",
"command": "-cursorRight",
"when": "textInputFocus"
},
{
"key": "shift+alt+l",
"command": "cursorLeft",
"when": "textInputFocus"
},
{
"key": "left",
"command": "-cursorLeft",
"when": "textInputFocus"
}
I've deleted all the commands and replaced them with this all seems to work enter code here
{
"key": "alt+r",
"command": "cursorRight",
"when": "textInputFocus"
},
{
"key": "Right",
"command": "cursorRight"
},
// down
{
"key": "alt+d",
"command": "cursorDown"
},
// UP
{
"key": "alt+u",
"command": "cursorUp"
},
// LEFT
{
"key": "alt+l",
"command": "cursorLeft",
},
{
"key": "Left",
"command": "cursorLeft"
},

Move Cursor Up or Down Multiple Lines at once in VS Code

Note: I have searched in Google and other SO posts, but none of them have helped me.
I know that we can move up or down with Alt+Arrow. Is there a way to move, let's say 25 lines up, at once? I don't want to press Alt+↑ 25 times.
Is there a plugin or a built-in feature to do this?
The reason I'm asking is that it's easier to move multiple lines at once due to the relative line numbers feature in VS Code.
I don't want to specify each number in keybindings.json (As seen here).
To make it easier to navigate the cursor in blocks of lines you could set a keybinding to jump 10 lines up or down at once (in your keybindings.json):
{
"key": "ctrl+up", // whatever keybinding you want
"command": "cursorMove",
"args": {
"to": "up",
"by": "line",
"value": 10 // change this if you want
},
"when": "editorTextFocus"
},
{
"key": "ctrl+down", // whatever keybinding you want
"command": "cursorMove",
"args": {
"to": "down",
"by": "line",
"value": 10 // change
},
"when": "editorTextFocus"
}
As noted in the comments, this comes from https://stackoverflow.com/a/48568520/836330 so upvote that.
You can use multi-command
Like in Photoshop when you move something Arrow moves 1 pixel, Shift+Arrow moves 20 pixels.
The Arrow key has no modifier combo unused so we have to choose a different key.
New keybindings to move up or down 10 lines
{
"key": "alt+numpad8", // or any other key combo
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction",
"editor.action.moveLinesUpAction"
]
},
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+numpad2",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction",
"editor.action.moveLinesDownAction"
]
},
"when": "editorTextFocus && !editorReadonly"
}
Maybe you have to add a little delay
"args": {
"interval": 50,
"sequence": [
I do not think there is an option to achieve what you want with VS Code only but there is an extremely powerful Vim plugin which is based on Vim terminal text editor. You should check it out, you can do anything you imagine with it but it takes some time to get used to it.
https://marketplace.visualstudio.com/items?itemName=vscodevim.vim

In VScode, is there a way to go next line per two or three with shortcut?

I usually use ctrl + n/p to go to next/previous line in VSCode. But it sometimes makes me feel dull because it has the cursor go just one line when I want to browse the file casually.Then, is there way to have the cursor go per a few lines or other more quick way to browse file(like go to next class or method definition)?
Jumping Lines in V.S. Code
V.S. Code Keyboard Shortcuts
        V.S. Code offers its users the ability to customize Keyboard Shortcuts using navigational type commands that move the cursor, via the "keybindings.json" configuration file. When the "keybindings.json" file is configured properly you are able to use Keyboard Shortcuts to jump up n-lines, jump down n-lines, jump left n-chars, or to jump right n-chars.
NOTE: "n can equal any number".
Getting Started
        To navigate the cursor in the fashoin that this topic covers, you will need to configure a few customized V.S. Code Keybindings (aka Keyboard Shortcuts). If this is not somthing that you have done before you don't need to worry. Binding a shortcut to a unique combonation of keys is actually quite simple, and it can be whimsical as well. It certainly will improve your V.S. Code experiance.
To get started creating your "line-jumping" Keyboard Shortcuts follow the instructions below. If you need any extra help during the keybinding creation process you can use the official V.S. Code, community maintained, documentation as a guide. I have posted the link below:
V.S. Code: Keyboard Shortcuts
– Use the F1 Key to open your "Quick Input Drop-Down Menu"
– Type "Keyboard Shortcut" into the "Quick Input Menu".
– Select "Open Keyboard Shortcuts (JSON)"
Note: There will be two "Keyboard Shortcut" options that you will be able to choose from. One of the options will read default, and one WILL NOT read default. Make sure to select the one that DOES NOT read default. If you are unfamiliar with keybindings, and have never created one before then the file should be totally empty.
Once inside of the keybindings.json file, add the following "JSON" block to the file
// "keybindings.json"
{
{
"key": "ctrl+1",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "down",
"value": 1
}
},
{
"key": "ctrl+2",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "down",
"value": 5
}
},
{
"key": "ctrl+3",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "down",
"value": 10
}
},
{
"key": "ctrl+shift+1",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "up",
"value": 1
}
},
{
"key": "ctrl+shift+2",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "up",
"value": 5
}
},
{
"key": "ctrl+shift+3",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "up",
"value": 10
}
},
}
"The above is only for testing purposes, as the keybindings are simple, and override default VSCode keybindings. You will have to decide which keybindings work best for you, however; if you add the above to your "keybindings.json" file you will get the following Keyboard Shortcuts"
Jump Down a Line      CTRL+1
Jump Down 5 Lines    CTRL+2
Jump Down 10 Lines  CTRL+3
Jump Up a Line      CTRL+SHIFT+1
Jump Up 5 Lines    CTRL+SHIFT+2
Jump Up 10 Lines  CTRL+SHIFT+3
For More Help:
For further assistance Visit the VSCode Commands Page to a list of all commands, or to read specifically about the cursorMove command #:
V.S. Code API Commands Read the Docs page.
...or to read about keybindings visit keybindings page that is included the opensource community maintained VSCode documentation collection #:
Creating V.S. Code Keybindings in your keybindings.json file
From the extension
Select By you can use the moveby.calculation command.
keybindings.json
{
"key": "ctrl+i ctrl+5", // or any other key binding
"when": "editorTextFocus",
"command": "moveby.calculation",
"args": {
"lineNrEx": "selection.start.line + 5",
"charNrEx": "selection.start.character"
}
}
Many thanks to #j D3V for his answer.
in new version of vsCode the right syntax for this combination is: forExample : ctrl+numpad1
this is edited json file that #j D3V mentioned in his answer :
// Place your key bindings in this file to override the defaults
[
{
"key": "ctrl+numpad1",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "down",
"value": 1
}
}
,
{
"key": "ctrl+numpad2",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "down",
"value": 5
}
},
{
"key": "ctrl+numpad3",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "down",
"value": 10
}
},
{
"key": "ctrl+shift+numpad1",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "up",
"value": 1
}
},
{
"key": "ctrl+shift+numpad2",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "up",
"value": 5
}
},
{
"key": "ctrl+shift+numpad3",
"when": "editorTextFocus",
"command": "cursorMove",
"args": {
"by": "line",
"to": "up",
"value": 10
}
},
]
As far as I know, there is no shortcut at the moment to skip multiple lines without extensions. However, ctrl + shift + o (by default) or prefixing your search query (ctrl + p by default) with an # can be used to navigate through symbols in a file, which might be what you were looking for.

How to scroll 10 line vertically with keyboard without moving cursor in vs code?

I am vim user, I don't like mouse to scroll.
pagedown and pageup is not perfect for me, I want to bindkey for pageup and pagedown with 10 lines scroll or something like that
Any idea how to do this?
You can use the editorScroll command to scroll the editor any amount you set without moving the cursor from its original position. For example, in keybindings.json:
{
"key": "alt+m", // whatever keybinding you like
"command": "editorScroll",
"args": {
"by": "line",
"to": "down",
// "revealCursor": false, // set to true if you did want to move the cursor
// false is the default
"value": 10
},
"when": "editorFocus"
},
{
"key": "shift+alt+m", // whatever keybinding you like
"command": "editorScroll",
"args": {
"by": "line",
"to": "up",
// "revealCursor": false,
"value": 10
},
"when": "editorFocus"
},
complex commands including editorScroll