Using Key Commands in Emacs

Using Key Commands In Emacs

If you have decided to take on Emacs as a more feature-rich terminal editor, or as a local text editor you can use to write programs or manage your server remotely, you will need to be familiar with how key commands and key bindings work. With Emacs, you will be using key bindings to run all kinds of functions. You may also be installing plugins that use their own set of key bindings. In this article, you will learn how key bindings and key commands work and how to read them in documentation, so you will know what to type.

Emacs comes pre-installed on cPanel hosting servers, but will require installation on unmanaged cloud hosting.

Make sure your business, agency, or reseller clients are always connected and powered on with our optimized Managed VPS Hosting.

The Emacs Language and Key Commands

In many ways, Emacs has its own sort of language. (And it is literally a programming language when referring to Emacs Lisp, a form of Lisp that Emacs is written in and which you can use to expand it.)

But in addition to Emacs Lisp, Emacs has its own unique way of organizing itself and representing the various concepts you need to know in order to use the program effectively. This is where some common commands and operating procedures can be intimidating to newcomers.

All new users should avail themselves of the Emacs walk-through tutorial, which comes preinstalled with all Emacs installations. You can access the tutorial at any time by Ctrl+h followed by the letter t.

When getting help in Emacs, you will often see usages of C and M keys on your keyboard. These represent the Ctrl and Alt keys on most contemptorary keyboard layouts. The Alt key is represented by an M because it was historically known as the “Meta” key, and this convention has stayed true until today.

So, basically, when you see C and M think Ctrl and Alt.

What is “Meta X”? – Launching Functions With The “Meta” Key

As you are reading Emacs documentation, you will often be advised to use the M-x or, “meta X” command followed by a function definition. For example, if you want to enable various modes, like “visual line mode” (to wrap text at the end of the window), you will be instructed to press M-x on your keyboard. As you will remember from the above section, this means pressing the Alt key on your keyboard followed the x key (lowercase), while still holding down Alt.

The instruction, in documentation, will look like this:

M-x visual-line-mode

Sometimes, you may see an instruction like this followed by a <RET> to indicate that you must press your Enter key in order to finish the process.

Using Keys in Combination

Often, important functions will be “bound” to various key combinations. These are called “key bindings”. Just about every mode in Emacs has certain key bindings associated with various functions. Often, function definitions can have long names. For example, the function definition for converting a region of text in Org mode to HTML is org-html-convert-region-to-html. These descriptive function definitions can be helpful when you need to quickly find out which function you are looking for, but it can be tedious to type. This is why many such function definitions are bound to a certain combincation of keys.

The most basic key binding you want to know is how to exit Emacs. When you want to exit Emacs, you will run this function:

save-buffers-kill-terminal

However, you will not want to type that every time you want to close your Emacs terminal or window. So this function is already bound to these keys:

C-x C-c

On your keyboard, this would be Ctrl+x followed by Ctrl+c.

Another Example, Movement Keys

One of the stumbling blocks many new Emacs users face is moving around in Emacs. By default, the arrow keys on your keyboard will move you around by lines and characters, just as in most text editors and word processing apps you will find on the market.

However, there are a series of key commands you can use to move around without having to leave the main typing area of your keyboard:

C-n (move down one line)
C-p (move up one line)
C-f (move forward one character)
C-b (move back one character)

You can see in the above example that not all key combinations involve multiple inputs. These functions all use one single key combination and give you an immediate result.

As you continue using Emacs, you will likely find occasions in which you will want to create your own key bindings. However, you will want to be careful not to create a binding that is already in use by another program. This will not break anything, but it might create a confusing situation in which you are instructed to put in a key binding and find it does something different.


Well done! You now know how the various key combinations and key bindings work in Emacs, and you know how to call various functions with M-x. These are some of the essential building blocks you will need to master when working with Emacs. If you have any questions or issues, feel free to leave a comment below.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!