Skip to content

Open search with Cmd+K / Ctrl+K - #4852

Closed
Sushants-Git wants to merge 1 commit into
rust-lang:mainfrom
Sushants-Git:cmd-k-search
Closed

Sushants-Git wants to merge 1 commit into
rust-lang:mainfrom
Sushants-Git:cmd-k-search

Conversation

@Sushants-Git

Copy link
Copy Markdown

mdBook's built-in search is currently only reachable with S or /, which many readers never discover. Cmd+K (macOS) / Ctrl+K (elsewhere) is the shortcut most documentation sites use for search, so this binds it to the existing search bar.

What it does

  • Adds cmdk-search.js (loaded via additional-js, next to ferris.js).
  • Cmd/Ctrl+K opens the search bar and focuses the input from anywhere on the page; if the bar is already open, it refocuses and selects the query.
  • Prevents the browser's default Cmd/Ctrl+K action (e.g. focusing the address bar) only when the search bar exists.
  • Shows the shortcut in the search placeholder and the toggle button's tooltip, and adds it to aria-keyshortcuts.

It reuses mdBook's existing search (index, ranking, keyboard navigation), so there's no new search UI or dependency. mdBook's own key handler ignores modified keys, so the existing S / / shortcuts are unaffected.

Testing

Built with mdBook 0.5.1 (the CI version) and checked in headless Chromium: Cmd+K opens and focuses the search bar, typing returns results, and pressing Cmd+K again while open refocuses the input. dprint check passes.

mdBook's built-in search is only reachable with S or /, which many
readers don't know about. Cmd+K (macOS) / Ctrl+K (elsewhere) is the
shortcut most documentation sites use for search, so bind it to the
existing search bar and mention it in the placeholder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant