Adding Vim-like Ctrl+J and Ctrl+K navigation to quick switcher as well as Alt+[0-9]

Save this as a .js file and point CodeScript ToolKit to it as your “Startup script path.” It’ll make it so pressing Ctrl+J moves down and Ctrl+K moves up in the default Obsidian quick switcher.

exports.invoke = async (app) => {
	const switcherPlugin = app.internalPlugins.getPluginById('switcher');

	if (!switcherPlugin?.instance) {
		console.error("Quick switcher plugin not found.");
	} else {
		const ModalClass = switcherPlugin.instance.QuickSwitcherModal;
		const originalOnOpen = ModalClass.prototype.onOpen;

		ModalClass.prototype.onOpen = function() {
			originalOnOpen.apply(this, arguments);

			const downEntry = this.scope.keys.find(k => k.key === 'ArrowDown' && k.modifiers === '');
			const upEntry = this.scope.keys.find(k => k.key === 'ArrowUp' && k.modifiers === '');

			if (downEntry && upEntry) {
				this.scope.register(['Ctrl'], 'J', (e) => downEntry.func.call(this, e));
				this.scope.register(['Ctrl'], 'K', (e) => upEntry.func.call(this, e));
			}
		};	
	}
}

If you’d like to also have Alt+[0-9] to switch to a specific quick switcher entry[1], add this directly under the if (downEntry && upEntry) section.

const enterEntry = this.scope.keys.find(k => k.key === 'Enter' && k.modifiers === '');

for (let i = 0; i <= 9; i++) {
	this.scope.register(['Alt'], `${i}`, (e) => {
		const index = i === 0 ? 9 : i - 1;
		const values = this.chooser?.values;

		if (values && values[index]) {
				this.chooser?.setSelectedItem(index, e);
			if (enterEntry) {
				enterEntry.func.call(this, e);
			}
		}
		return false;
	});
}

I saw this and this. I thought it was so neat how they essentially copied the function that runs when certain keys are pressed and put it under different keys. I then remembered how I accessed core plugin stuff for an audio recorder toggle

Recently I had stopped using the Quick Switcher++ community plugin and was missing the Ctrl+J/Ctrl+K navigation as well as the Alt+[0-9] to jump directly to a specific file in the list, so decided to bring them back.

Relevant feature request: Ctrl-J / Ctrl-K Hotkey for navigation within command palette and quick switcher - Feature requests - Obsidian Forum
There are some other workarounds in that thread, but they add EventListeners to the entire document which felt like overkill to me.


  1. 0 being the 10th entry. ↩︎