7

How can I configure Emacs to set the default input method for all new buffers? I'd like to use polish-slash everywhere.

I use Emacs 24.3.1.

I am interested in a solution which could be pasted into init.el so I can easily reuse this config between my machines.

Using just (setq default-input-method "polish-slash") in init.el does not work for me:

  • C-h v default-input-method returns "polish-slash".
  • C-h v current-input-method returns nil in probably any buffer. I checked init.el and *scratch*.

1 Answer 1

9

Setting default-input-method

In the Customize interface (M-x customize or “Options” → “Customize Emacs” → “Top-level Cutomization Group”), navigate to “Environment” → “I18n” → “MULE”. Set the “Default Input Method”: choose “String” in the value menu, and set the string to polish-slash. Use the “State” button to set the value for the current session or to save it.

You can also reach this Customize setting M-x customize-variable or through the menu “Options” → “Customize Emacs” → “Specific Option”, then entering default-input-method.

Alternatively, set the default-input-method variable in your .emacs:

(setq default-input-method "polish-slash")

Once this is done, polish-slash will be the default input method. However, by default, no input method is active (which is different, internally, from some kind of “none” or “pass-through” input method). The default input method is the one that gets activated by typing C-\ (toggle-input-method).

Activating the input method by default

If you want the input method to be turned on by default, you need an additional step. I can't find a high-level way to do it. One low-level way is to add the following lines to your init file:

(defvar use-default-input-method t)
(make-variable-buffer-local 'use-default-input-method)
(defun activate-default-input-method ()
  (interactive)
  (if use-default-input-method
      (activate-input-method default-input-method)
    (inactivate-input-method)))
(add-hook 'after-change-major-mode-hook 'activate-default-input-method)
(add-hook 'minibuffer-setup-hook 'activate-default-input-method)
(defun inactivate-default-input-method ()
  (setq use-default-input-method nil))
(add-hook 'c-mode-hook 'inactivate-default-input-method)

The variable use-default-input-method and the function inactivate-default-input-method provide a way to turn off the input method (it may be annoying e.g. in programming modes). If you want to turn off the default method in some modes, you register the function inactivate-default-input-method in that mode's startup hook, as in the example above for C mode and derived modes (C++, Java, etc.).

The code above systematically turns on the default input method in the minibuffer (comment out that line to have it off by default). Smarter code might use the same input method or lack thereof as the buffer that was previously active, or might choose an input method depending on what the minibuffer is prompted for (e.g. used for search strings but not for file names), but I declare this beyond the scope of this answer.

If you prefer to whitelist modes where the input method is automatically activated, add the function activate-default-input-method below to these modes' startup hook, as in the example below for Text mode and derived modes.

(defun activate-default-input-method ()
  (interactive)
  (activate-input-method default-input-method))
(add-hook 'text-mode-hook 'activate-default-input-method)
2
  • 1
    You can also reach the customization group directly by M-x customize-variable followed by default-input-method at the prompt.
    – Vamsi
    Commented Sep 28, 2014 at 23:02
  • In addition to finding this option by browsing M-x customize, if you use M-x customize-variable then completion is available. If you type default TAB then you can find it among the options with names that start with "default" (there are only 3). What if you don't know that the name starts with "default"? If you use a library that enhances completion a bit then you can type input-method TAB instead. Or perhaps method and input. IOW, completion, like browsing Customize from the top, is a way to find an option.
    – Drew
    Commented Sep 29, 2014 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.