Run commands by typing in the quick switcher

Disclaimer

Is this project open source? Yes
Is this project completely free? Yes
Is this project vibe-coded beyond the author’s ability to comprehend how it works? No

Save this as a .js file and point CodeScript ToolKit to it as your “Startup script path.” It allows you to run command palette commands[1] by typing in Obsidian’s quick switcher.

I put in some defaults/examples, but of course you can change them, add to them, or remove them.

The defaults:

  • Type “>” to open command palette
  • Type “?” to search the current note
  • Type “+” to open the current file in your device’s regular application for that file type[2]
  • Type “.” to toggle reading view
exports.invoke = async (app) => {
	app.workspace.onLayoutReady(() => {
		const switcherPlugin = app.internalPlugins.getPluginById('switcher');
		if (!switcherPlugin?.instance) return;

		const ModalClass = switcherPlugin.instance.QuickSwitcherModal;
		
		if (ModalClass.prototype.onOpen.__listenerAdded) {
			return; 
		}

		const originalOnOpen = ModalClass.prototype.onOpen;
		const originalOnClose = ModalClass.prototype.onClose;

		const actions = {
			'>': () => app.commands.executeCommandById('command-palette:open'),
			'?': () => app.commands.executeCommandById('editor:open-search'),
			'+': () => app.commands.executeCommandById('open-with-default-app:open'),
			'.': () => app.commands.executeCommandById('markdown:toggle-preview')
		};

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

			if (this._customInputHandler && this.inputEl) {
				this.inputEl.removeEventListener('input', this._customInputHandler);
			}

			this._customInputHandler = (e) => {
				const quickSwitcherInput = e.target.value;
				
				if (actions[quickSwitcherInput]) {
					e.target.value = '';
					this.close();
					actions[quickSwitcherInput]();
				}
			};

			this.inputEl.addEventListener('input', this._customInputHandler);
		};
		ModalClass.prototype.onOpen.__listenerAdded = true;

		ModalClass.prototype.onClose = function() {
			if (this.inputEl && this._customInputHandler) {
				this.inputEl.removeEventListener('input', this._customInputHandler);
				this._customInputHandler = null;
			}
			
			if (originalOnClose) {
				originalOnClose.apply(this, arguments);
			}
		};
	});
}

  1. Or whatever else you want. ↩︎

  2. On mobile it opens the share menu. ↩︎