Emacs Practitioners Guide to Emacs Editor

Emacs Cheatsheet

Emacs is a highly versatile and flexible text editor that offers a lot of powerful extensions and packages to its users. It is often considered to have a very steep learning curve, and with having so many things installed, it becomes hard to keep track of them all.

Hence, we have created this Emacs cheatsheet to help smoothen your experience.

Terminology

To make commands easier to understand, Emacs has a few abbreviations that need to be clarified before moving onto looking at some of its necessary commands.

The following specifies what each terminology means in terms of key bindings:

  • C-x means: click on Control and x at the same time
  • M-x means: type Alt then x; M here refers to Meta

Accessing Help

Emacs has a pretty detailed and straightforward help system. For first time users, it offers a built-in tutorial that can be accessed by using Ctrl + h followed by t keys.

By hitting Ctrl + h followed by r, users can get access to the extremely detailed manual of Emacs.

Basic File Handling Commands

To open or create a file, hit the keys Ctrl + x followed by Ctrl + f and then enter the filename. If a file exists with that name, it will open that file. However, if a file with that name isn’t present, then Emacs will create a new file with that name.

To replace this currently opened file with another, hit the keys Ctrl + x followed by Ctrl + v and enter the filename of the new file.

A file can be saved with Ctrl + x, followed by Ctrl + s. To save a file with a different name, use the shortcut Ctrl + x followed by Ctrl + w and then enter the new filename.

The shortcut Ctrl + x, followed by i, can also be used to insert the contents of another file into your current file.

To check the number of files or buffers opened, hit Ctrl + x, followed by Ctrl + b.

To switch from one file to another, hit Ctrl + x, followed by b.

To close files, hit Ctrl + x, followed by k. If no file is specified, then the default file is closed.

To quit Emacs, hit Ctrl + x followed by Ctrl + c.

Moving Around

Similar to Vim, Emacs offers shortcuts for cursor movements as an alternative to using the mouse. The following table describes these in detail

Keys Movement
Movement By Forward Backward
Characters Ctrl + f Ctrl + b
Words Alt + f Alt + b
Lines Ctrl + n Ctrl + p
Sentences Alt + e Alt + a
Paragraphs Alt + } Alt + {
End/start of line Ctrl + e Ctrl + a
End/start of buffer Alt + > Alt + <
Scrolling Ctrl + v (Down) Alt + v (Up)

To display line numbers in your Emacs, hit Alt + x and enter linum-mode. Now using this, you can go to a specific line number by hitting Alt + g followed by g and then entering the line number. For example:

Editing Tools

Emacs is quite a popular text editor, and that is mainly due to the amazing editing tools it provides to its users. One of these is the ability to repeat a command by the length specified. To do this, hit Ctrl + u followed by entering the length of repeating and then entering the command to be repeated. For example, if I want to scroll three pages down, then this would be:

Before being able to apply commands like copy and cut, we first have to select the region on which these commands will be applied. This is done by Ctrl + Space. To mark the whole region that you want to select, drag the cursor to the end-point. For example, in the image below, the rectangle specifies the beginning of the selection and the circle its end.

To select the contents of the whole file, hit the keys Ctrl + x followed by h.

After selecting, you can apply the copy command on this using the keys Alt + w. The cut command provides you with several options. To cut a single word, hit Alt + d. To cut a single line, hit Ctrl + k. To cut the whole selected region, hit Ctrl + w.

Text can be pasted by using the shortcut Ctrl + y. You can cycle around the killed items by using the key Alt + y.

Latest Cut Item:

Using Alt + y to replace this with old killed item:

Undo and redo operations can be applied by Ctrl + / and Ctrl + g followed by Ctrl + / keys respectively.

To increase the text size of the screen, click the keys Ctrl + x followed by Ctrl + + (plus) and decrease the text size by Ctrl + x followed by Ctrl + – (minus).

Searching and Replacing Text

To perform a simple incremental search, use the keys Ctrl + s. Jump from one result to another by continuously pressing this key.

To perform this incremental search in reverse, use the keys Ctrl + r.

For regular expression, search to take place, hit the keys Ctrl + Alt + s.

To perform a simple query, replace, use the keys Alt + Shift + 5.

Asking for the word to be replaced:

Asking for the word to replace with:

Similarly, Emacs also lets users perform a replacement of regular expressions using the keys Ctrl + Alt + Shift + 5.

Error Recovery

Emacs has some great commands that offer aid in recovering or restoring the contents of a file. To abort commands that are either running or have been partially typed, click Ctrl + g.

To recover a file whose data might have been lost, use the keys Alt + x to open the mini-buffer and then enter recover-file.

To recover the entire session, then hit Alt + x and enter recover-session.

To revert a changed file to its original form, hit Alt + x and enter revert-buffer.

Accessing External Tools

The powerful nature of Emacs is seen from the fact that it makes integration with multiple external tools possible. This is one of the reasons why it is considered to be more than a text editor.

To open the shell inside Emacs, hit Alt + x and enter the shell.

Emacs also has the GDB debugger built-in, which can be opened by hitting Alt + x and entering gdb.

Emacs also offers interesting games such as tetris, solitaire to its users. To open tetris, simply hit Alt + x and enter tetris.

Command:

Result:

Conclusion?

To keep track of all the commands in Emacs, a cheat sheet becomes quite necessary to have since Emacs has lots of tools inside of it, and accessing them through the menu is quite the hassle.

About the author

Zeeman Memon

Zeeman Memon

Hi there! I'm a Software Engineer by degree, Blogger by skills who loves to write about tech, develop websites & do SEO. You can reach out to me on LinkedIn.