0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCodeをキーボードだけで快適に ─ キャレット迷子を防ぐスクロール設定

Last updated at Posted at 2025-03-29

はじめに

キーボード操作だけでVSCodeを使いたい時、ちょっとした違和感を覚える場面があります。

Ctrl + ↑ / Ctrl + ↓ で画面をスクロールできるものの、キャレットの位置がスクロール外に残ったままのため、編集のためキーボード操作を行うと、もともとキャレットがあった行に戻ってしまいます。 これを防ぐため、画面のどこか一部をマウスでクリックし、迷子になったキャレットを呼び戻してあげる必要があります。せっかくキーボード操作でスクロールできるのに惜しい。

解決方法

keybindings.json に以下を追加します:

[ ctrl + p ] keybindings.json
{
    "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"
}

設定ファイルの開き方

  1. Ctrl + Shift + P でコマンドパレットを開く
  2. Open Keyboard Shortcuts (JSON) を選択
  3. 開いた keybindings.json に追加

revealCursor: true の意味

今回の問題を解決するのが revealCursor: true の設定です。これを有効にすると、スクロールしてもキャレットが画面内に追随するようになります。Vimでは当然の挙動ですが、これで同様になります。わざわざこのような設定を内蔵しているのであれば、デフォルトで有効にしておいてほしいですが。

なお、editor.stickyScroll.enabled が有効になっている場合、編集再開時にスクロール位置が微妙にずれることがあります。上部にスティッキー表示されるスコープ行の行数が内部的に考慮されてないことが原因のようですが、これを補正する方法は現時点ではなさそうです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?