How to change the keyboard layout between English and Spanish quickly in Emacs to be able to write accents and other Spanish letters.

Overview

If you write in Spanish and English then you will have to choose between using a keyboard with Spanish or English layout. No matter which one you select, when you write in the other language you will have to choose a strategy to handle the letters that are present in one language that are not in the other one.

This is a quick overview to handle this situation with an English keyboard layout (i.e.: not having , and tildes , , etc)

Changing the keyboard layout

In Emacs you can quickly change the layout with set-input-method, which will change the keyboard layout for the current buffer.

set-input-method: Select and activate input method INPUT-METHOD for the current buffer. This also sets the default input method to the one you specify.

mule-cmds.el Emacs manual

There are three alternatives for Spanish:

  1. spanish-keyboard
  2. spanish-postfix
  3. spanish-prefix

Check layouts

What layout each keyboard uses? Let鈥檚 find out with quail-show-keyboard-layout, in each input method it prints the recognized layout:

  1. spanish-keyboard
Keyboard layout (keyboard type: standard)

     +----------------------------------------------------------------+
      | 1 ! | 2 " | 3 路 | 4 $ | 5 % | 6 & | 7 / | 8 ( | 9 ) | 0 = | ' ? | 隆 驴 | 铆 脥 |
     +----------------------------------------------------------------+
        | q Q | w W | e E | r R | t T | y Y | u U | i I | o O | p P | 茅 脡 | 贸 脫 |
       +------------------------------------------------------------+
         | a A | s S | d D | f F | g G | h H | j J | k K | l L | 帽 脩 | 谩 脕 | 煤 脷 |
        +-----------------------------------------------------------+
           | z Z | x X | c C | v V | b B | n N | m M | , ; | . : | - _ |
          +-------------------------------------------------+
		    +-----------------------------+
		    |          space bar          |
		    +-----------------------------+
  1. spanish-postfix
Keyboard layout (keyboard type: standard)

     +----------------------------------------------------------------+
      | 1 ! | 2 @ | 3 # | 4 $ | 5 % | 6 ^ | 7 & | 8 * | 9 ( | 0 ) | - _ | = + | ` ~ |
     +----------------------------------------------------------------+
        | q Q | w W | e E | r R | t T | y Y | u U | i I | o O | p P | [ { | ] } |
       +------------------------------------------------------------+
         | a A | s S | d D | f F | g G | h H | j J | k K | l L | ; : | ' " | \ | |
        +-----------------------------------------------------------+
           | z Z | x X | c C | v V | b B | n N | m M | , < | . > | / ? |
          +-------------------------------------------------+
		    +-----------------------------+
		    |          space bar          |
		    +-----------------------------+
  1. spanish-prefix
Keyboard layout (keyboard type: standard)

     +----------------------------------------------------------------+
      | 1 ! | 2 @ | 3 # | 4 $ | 5 % | 6 ^ | 7 & | 8 * | 9 ( | 0 ) | - _ | = + | ` ~ |
     +----------------------------------------------------------------+
        | q Q | w W | e E | r R | t T | y Y | u U | i I | o O | p P | [ { | ] } |
       +------------------------------------------------------------+
         | a A | s S | d D | f F | g G | h H | j J | k K | l L | ; : | ' " | \ | |
        +-----------------------------------------------------------+
           | z Z | x X | c C | v V | b B | n N | m M | , < | . > | / ? |
          +-------------------------------------------------+
		    +-----------------------------+
		    |          space bar          |
		    +-----------------------------+

Choosing the layout

  • spanish-keyboard: while having all the typical Spanish letters their own key, I don鈥檛 find it pretty close to any other keyboard I have ever tried.

  • spanish-postfix: expects to press tildes after the desired letter, for example o+tilde=贸 which I find a bit unnatural.

  • spanish-prefix: The one that resembles more the classical Spanish keyboard behaviour is the spanish-prefix one. In this one you will write tildes before the letter, e.g.:

    • to write : press the tilde key and then the o.
    • to write : press the ~ symbol and then o

Conclusion

After executing set-input-method and selecting spanish-prefix, writing in each language is as easy as switching them with C-\ (toggle-input-method) and have a pretty close experience as using a Spanish keyboard in an English keyboard layout.

toggle-input-method:

Enable or disable multilingual text input method for the current buffer. Only one input method can be enabled at any time in a given buffer.

mule-cmds.el Emacs manual
Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.