Skip to content

Add vim key bindings for navigation#3072

Open
cianswords wants to merge 2 commits intorust-lang:masterfrom
cianswords:add-vim-bindings
Open

Add vim key bindings for navigation#3072
cianswords wants to merge 2 commits intorust-lang:masterfrom
cianswords:add-vim-bindings

Conversation

@cianswords
Copy link
Copy Markdown

This change adds Vim-style key bindings for easier navigation without leaving the home row.

Implemented are:

  • h and l for going to the previous and next page
  • j and k for scrolling up and down
  • d and u for scrolling up and down by one page

(Note: This is my first ever PR, please tell me if I did something wrong)

closes #2936

Copilot AI review requested due to automatic review settings April 16, 2026 19:14
@rustbot rustbot added the S-waiting-on-review Status: waiting on a review label Apr 16, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds Vim-style keyboard shortcuts to mdBook’s HTML output to improve navigation and scrolling from the keyboard.

Changes:

  • Document Vim-style shortcuts in the user guide.
  • Update the in-app help popup to list the new shortcuts.
  • Implement h/l chapter navigation and j/k/d/u scrolling in the frontend key handler.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
guide/src/guide/reading.md Documents the new Vim-style key bindings.
crates/mdbook-html/front-end/templates/index.hbs Updates the keyboard shortcut help text shown in the UI.
crates/mdbook-html/front-end/js/book.js Adds key handling for h/l/j/k/d/u to navigate/scroll.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

View changes since this review


The **left and right arrow keys** on the keyboard can be used to navigate to the previous or the next chapter.

Additionally, Vim-style key bindings (<kbd>H</kbd>, <kbd>J</kbd>, <kbd>K</kbd>, <kbd>L</kbd>, <kbd>D</kbd> and <kbd>U</kbd>) can be used for navigation.
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line says the Vim-style keys are used for “navigation”, but in the implementation j/k/d/u scroll rather than navigate between chapters. Also, the doc displays uppercase keys, while the key handler matches lowercase ('h', 'j', etc.), which may mislead users into holding Shift. Recommend updating the text to distinguish chapter navigation (h/l) vs scrolling (j/k and d/u), and either show lowercase in the guide or add uppercase cases in the handler.

Suggested change
Additionally, Vim-style key bindings (<kbd>H</kbd>, <kbd>J</kbd>, <kbd>K</kbd>, <kbd>L</kbd>, <kbd>D</kbd> and <kbd>U</kbd>) can be used for navigation.
Additionally, Vim-style key bindings can be used: <kbd>h</kbd> and <kbd>l</kbd> navigate to the previous and next chapter, while <kbd>j</kbd>, <kbd>k</kbd>, <kbd>d</kbd>, and <kbd>u</kbd> scroll the current page.

Copilot uses AI. Check for mistakes.
Comment on lines +739 to +754
case 'j':
e.preventDefault();
window.scrollBy({ top: 80, behavior: 'smooth' });
break;
case 'k':
e.preventDefault();
window.scrollBy({ top: -80, behavior: 'smooth' });
break;
case 'd':
e.preventDefault();
window.scrollBy({ top: window.innerHeight, behavior: 'smooth' });
break;
case 'u':
e.preventDefault();
window.scrollBy({ top: -window.innerHeight, behavior: 'smooth' });
break;
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These scroll actions always use behavior: 'smooth'. For users who prefer reduced motion, this can be an accessibility issue. Consider detecting prefers-reduced-motion: reduce and using behavior: 'auto' (or omitting behavior) when reduced motion is requested.

Copilot uses AI. Check for mistakes.
Comment on lines +739 to +754
case 'j':
e.preventDefault();
window.scrollBy({ top: 80, behavior: 'smooth' });
break;
case 'k':
e.preventDefault();
window.scrollBy({ top: -80, behavior: 'smooth' });
break;
case 'd':
e.preventDefault();
window.scrollBy({ top: window.innerHeight, behavior: 'smooth' });
break;
case 'u':
e.preventDefault();
window.scrollBy({ top: -window.innerHeight, behavior: 'smooth' });
break;
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scroll behavior is duplicated across four cases and introduces a magic number (80). This will be harder to tune consistently later. Consider extracting a small helper (e.g., scroll(delta)) and defining named constants for the line-step and page-step to centralize the behavior.

Copilot uses AI. Check for mistakes.
<div>
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
<p>Press <kbd>←</kbd>/<kbd>→</kbd> or <kbd>h</kbd>/<kbd>l</kbd> to navigate between chapters</p>
<p>Press <kbd>j</kbd>/<kbd>k</kbd> or <kbd>d</kbd>/<kbd>u</kbd> to scroll the page up and down</p>
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second line lumps j/k (small increments) and d/u (page-sized increments) together as “scroll up and down”. To avoid confusion, consider clarifying that j/k scroll by a small step and d/u scroll by roughly one page.

Suggested change
<p>Press <kbd>j</kbd>/<kbd>k</kbd> or <kbd>d</kbd>/<kbd>u</kbd> to scroll the page up and down</p>
<p>Press <kbd>j</kbd>/<kbd>k</kbd> to scroll a small amount, or <kbd>d</kbd>/<kbd>u</kbd> to scroll roughly one page</p>

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: waiting on a review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hjkl navigation

3 participants