Emacs

Emacs Key Bindings

We all appreciate the high customization and extensible nature of the Emacs text editor. Emacs offers powerful features coupled with the intensive use of keyboard shortcuts, providing the users with quick access and a workaround.

If you are new to Emacs, these keyboard shortcuts can be challenging to master, especially when you are unsure of what the different commands do and the various keyboard shortcuts that you can use. To help clear the confusion, this post focuses on the Emacs key bindings. We will discuss the commonly used keys while giving examples whenever applicable.

Different Categories and Examples of Emacs Key Bindings

At first, Emacs can seem more complicated than the other text editors. With Emacs, everything can be done using key bindings, and mastering your way of using the keys is stressful. However, we curated the most common keys that you should know and arrange them based on their functionality. This breakdown will give you comfort and ease as you get started with Emacs.

1. Modifier Keys

Emacs uses two modifier keys. The first is the Ctrl (control). The other is the Meta key (Alt/Esc). The “Ctrl” key is used with basic commands to trigger an event. For instance, you can trigger the search feature by pressing “C-x x-f”. In such a case, you press the “Ctrl + x” keys simultaneously, then release and press the “Ctrl + f” keys to bring up the search prompt.

You will get an output like the one in the following:


The “Meta” key is combined with other keys to trigger the commands. For instance, you can use the Emacs command prompt by pressing “M-x”. Press the “Alt + x” keys simultaneously and then type your command.

2. Basic Movements

Using the control/Ctrl and Meta (Alt) keys, you can quickly navigate a buffer.

    • C-p – It moves the cursor up.
    • C-n – It moves the cursor down.
    • C-b – It moves the cursor left.
    • C-f – It moves the cursor right.
    • C-a – It moves to the start of the line.
    • C-e – It moves to the end of the line.
    • M-f – It moves the cursor forward one word.
    • M-b – It moves the cursor back one word.

3. Text Keys

You can quickly select, copy, cut, and paste the texts with the following keys:

    • C-SPC (Ctrl + Space) – Start selecting the text. You will get an output that the mark has been set.
    • M-w – Copy the selected text
    • C-w – Cut the selected text
    • C-y – Paste the text from the clipboard

4. Search Features

Emacs lets you search within the text. You can also search for files when opening them.

    • C-s – Increments a search forward
    • C-r – Increments a search backward
    • C-x C-f – It lets you search and open a file.

5. Working with the Buffer

Emacs works with buffers and windows, and you can have multiple open buffers. Such a case requires you to know what keys to use.

  • C-x b – Switch between open buffers
      • C-x 1 – Close all open windows except the active one
      • C-x 2 – Split a window horizontally

  • C-x 3 – Split a window vertically
  • C-x C-s – Save the changes made to the current buffer
  • C-x C-c – Exit Emacs. If any buffer is open, it prompts you whether to save it before exiting.

    6. Customization and Commands

    You can customize Emacs using the configuration file. The configuration file is either “.emacs” or “.emacs.d/init.el”.

    To invoke a command such as the “text-scale-adjust”, press the “M-x”. Then, type the command’s name. Press the Enter/RET key to run the command.


    You can kill the current command by pressing the “C-g” keys.


    To undo the changes, use the “C-x u” keys.

    Conclusion

    Emacs is a powerful text editor, thanks to the numerous key bindings that you can utilize to perform the operations quickly. We discussed the commonly used Emacs key bindings to get you up-to-speed using the Emacs text editor. With that, you are ready to use and level up your usage and experience. Feel free to practice more key bindings; there are tons of them. Happy editing!

    About the author

    Denis Kariuki

    Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.