VS Code如何设置Ctrl+上下箭头时光标随页面同步移动?
Hey Luca, great question! The behavior you're describing—where Ctrl+arrow keys stop at the page edge and jump back when you release Ctrl—is super common in many text editors, but adjusting it to match Visual Studio's cursor-anchoring behavior is totally doable. Let's break down how to fix this depending on what editor you're using:
VS Code lets you customize keybindings extensively to replicate VS's behavior:
- Open the Keyboard Shortcuts editor by pressing
Ctrl+K Ctrl+S - Search for "cursorMove" commands, specifically the ones with
ctrl: trueandup/downarguments - You'll want to modify the existing bindings to add the
scrollType: "cursorAnchor"parameter. For example, the default binding for Ctrl+Down might look like this:{ "key": "ctrl+down", "command": "cursorMove", "args": { "to": "down", "by": "line", "value": 1, "ctrl": true }, "when": "textInputFocus && !editorReadonly" } - Edit it to include
"scrollType": "cursorAnchor"in the args object:{ "key": "ctrl+down", "command": "cursorMove", "args": { "to": "down", "by": "line", "value": 1, "ctrl": true, "scrollType": "cursorAnchor" }, "when": "textInputFocus && !editorReadonly" } - Repeat this adjustment for the
ctrl+upbinding. This will make the cursor stay anchored to the top (or bottom) of the viewport as you scroll, just like in Visual Studio.
Most modern editors support custom keybindings or have plugins that can mimic this behavior:
- Sublime Text: Head to
Preferences > Key Bindingsto create custom mappings. To get the cursor anchor effect, you might need a plugin likeScroll Cursor Anchoror write a small Python snippet to tie cursor movement to viewport anchoring. - Atom: Navigate to the
Keybindingssection in Settings, and install packages likescroll-cursor-anchor—they add this exact behavior out of the box.
If you're using an app that doesn't have built-in customization, tools like AutoHotkey (Windows) or Karabiner-Elements (macOS) can help remap Ctrl+arrow keys to simulate VS-style scrolling:
- For AutoHotkey, a simplified script might look like this (tweak it to match your app):
This script ties cursor movement to wheel scrolling, keeping the cursor anchored as you navigate.^Down:: Send {Down} WinGetActiveTitle, ActiveApp ; Target your editor of choice if (ActiveApp contains "Visual Studio Code" or ActiveApp contains "Visual Studio") { Send {WheelDown} } return ^Up:: Send {Up} WinGetActiveTitle, ActiveApp if (ActiveApp contains "Visual Studio Code" or ActiveApp contains "Visual Studio") { Send {WheelUp} } return
内容的提问来源于stack exchange,提问作者Luca

