Skip to content

Instantly share code, notes, and snippets.

@dandrake
Last active December 9, 2025 20:59
Show Gist options
  • Select an option

  • Save dandrake/b1f84d67f94b0a3fd20f5ee2fb8b5777 to your computer and use it in GitHub Desktop.

Select an option

Save dandrake/b1f84d67f94b0a3fd20f5ee2fb8b5777 to your computer and use it in GitHub Desktop.
"do command in other window" emacs lisp
(defun my/do-command-other-window (count command &optional return-p)
"Do COMMAND in other-window. The COUNT argument is provided to
`other-window'.
The optional RETURN-P argument, if true, will make this return to the
original window after calling COMMAND.
The intended way to use this function is by calling it from an
interactive function that specifies the command:
(defun do-mycommand-other-window (prefix)
(interactive \"P\")
(dd/do-command-other-window prefix 'mycommand))
For commands like `isearch' or `occur', you likely want to remain in the
other window; but for a command like `highlight-regexp', you may want to
remain in the original window and just have the regexp highlighted in
other-window. You can achieve that by passing t for RETURN-P.
"
(interactive)
(unless (one-window-p)
(if return-p
(save-window-excursion
(other-window count)
(call-interactively command))
(progn
(other-window count)
(call-interactively command)))))
(defun my/occur-other-window (prefix)
"Do `occur' in other-window.
With prefix argument, do the previous window."
(interactive "P")
(dd/do-command-other-window (if prefix 1 -1) 'occur))
;; you can define similar wrappers for isearch-forward, isearch-backward, and so on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment