NonGNU ELPA - opam-switch-mode


Select OCaml opam switches via a menu
opam-switch-mode-1.7.tar (.sig), 2024-Mar-31, 40.0 KiB
Atom feed
Browse ELPA's repository
CGit or Gitweb

To install this package from Emacs, use package-install or list-packages.

Full description



Provide a command opam-switch-set-switch to change the opam switch of the running Emacs session, and a minor mode opam-switch-mode to select the opam switch via a (menu-bar or mode-bar) menu.

The menu is generated each time the minor mode is enabled and contains the switches that are known at that time. If you create a new switch, re-enable the minor mode to get it added to the menu. The menu contains an additional entry "reset" to reset the environment to the state when Emacs was started.

Installing opam-switch-mode

We recommend to install this mode from either the NonGNU ELPA or the MELPA repository of Emacs packages. In the sequel, we assume you have already set up those in your .emacs.

If you use the use-package macro, the recommended configuration is as follows:

(use-package opam-switch-mode
  :ensure t
  ((coq-mode tuareg-mode) . opam-switch-mode))

If you don't use use-package, do the following instead:

(add-hook 'coq-mode-hook #'opam-switch-mode)
(add-hook 'tuareg-mode-hook #'opam-switch-mode)

so that the minor mode is automatically enabled when coq-mode or tuareg-mode is on, see also opam-switch-mode aware modes.

Command opam-switch-set-switch

Invoke with M-x opam-switch-set-switch RET.

Choose and set an opam switch.

Set opam switch SWITCH-NAME, which must be a valid opam switch name. When called interactively, the switch name must be entered in the minibuffer, which forces completion to a valid switch name or the empty string.

Setting the opam switch for the first time inside Emacs will save the current environment. Using the empty string for SWITCH-NAME will reset the environment to the saved values.

The switch is set such that all process invocations from Emacs respect the newly set opam switch. In addition to setting environment variables such as PATH and CAML_LD_LIBRARY_PATH, this also sets exec-path, which controls Emacs' subprocesses (call-process, make-process and similar functions).

Just before the switch is changed, opam-switch-before-change-opam-switch-hook runs. After the switch is changed, opam-switch-change-opam-switch-hook runs. One of these can be used to inform other modes that run background processes that depend on the currently active opam switch.

For obvious reasons, opam-switch-set-switch will only affect Emacs and not any other shells outside Emacs.

opam-switch-mode aware modes

  • coq-mode from proof-general can kill the Coq background process when the opam switch changes, see option coq-kill-coq-on-opam-switch.
  • tuareg-mode from tuareg can kill the OCaml background process when the opam switch changes, see option tuareg-kill-ocaml-on-opam-switch.
  • merlin-mode from merlin can kill the underlying Merlin server when the opam switch changes, see option merlin-stop-server-on-opam-switch.

Old versions

opam-switch-mode-1.6.tar.lz2023-Jul-147.47 KiB
opam-switch-mode-1.5.tar.lz2023-Jul-127.39 KiB
opam-switch-mode-1.4.tar.lz2023-Jul-117.27 KiB
opam-switch-mode-1.1.tar.lz2023-Jun-216.00 KiB



All notable changes to opam-switch-mode will be documented in this file, in reverse chronological order.

The format is based on Keep a Changelog.

[1.7] - 2023-07-26

  • Mention Tuareg and Merlin, regarding opam-switch-mode support
  • Remove noise (empty headings) in at release time
  • (opam-switch--get-switches): Use command opam switch -s, so we avoid regexp filtering code, closes #15

[1.6] - 2023-07-14

  • Use command opam env --switch=$it --set-switch, so the env var $OPAMSWITCH is also set

[1.5] - 2023-07-12

  • Use vanilla (not GitHub-flavored) Markdown syntax for snippets
  • Replace (redraw-display) with (force-mode-line-update t)
  • Memoize (opam-switch-mode-lighter) to speed-up mode-line updates

[1.4] - 2023-07-11

  • (opam-switch-mode-lighter): Indicate the switch name in the minibuffer, closes #11
  • Document the feature from [1.3]

[1.3] - 2023-07-11

  • New hook: opam-switch-before-change-opam-switch-hook
  • (opam-switch-set-switch): Call (run-hooks 'opam-switch-before-change-opam-switch-hook) before changing the env

[1.2] - 2023-07-11

  • Changelog
  • Markdown badges
  • (opam-switch--reset-env) unexpectedly cleared exec-path, closes #13
  • (opam-switch--get-current-switch) returned "_opam" for local switches
  • (opam-switch--set-env) raised "No opam-root directory in PATH" for local switches, closes #12
  • Replace menu-bar's name (s/Opam-switch/OPSW/) so it matches mode-bar's name
  • Change mode-bar's first element, so it expands to "OPSW - Opam Switch Mode"
  • auto-update

[1.1] - 2023-06-20

  • new script

[1.0] - 2022-11-15

  • Initial release, distributed on MELPA.