Emacs Practitioners Guide to Emacs Editor

Emacs Shortcuts

Emacs is a highly versatile text editor that is extremely fast and has a very user-friendly interface to go along with it. Emacs is one of those editors whose users rely heavily on using shortcuts and keystrokes to do their work.Hence, in this article, we will be looking at some of the keyboard shortcuts that come along with Emacs.

Accessing the Help Section

The Emacs manual has some pretty great descriptions of the type of commands and features you can access in Emacs.

To open the manual inside Emacs, hit the keys Ctrl + h followed by r.

For users that prefer following a tutorial and guidelines, they can access Emacs’ built-in tutorial by clicking on Ctrl + h followed by t.

Basic File Shortcuts

For opening or creating files, the shortcut used is Ctrl + x followed by Ctrl + f and then entering the filename. If there is already a file present with the specified name, then Emacs will open that file otherwise it will create a new file with the name specified.

To visit a different file, then hit the keys Ctrl + x followed by Ctrl + v, and enter the filename of the new file.

Users can save their files by using the keys Ctrl + x followed by Ctrl + s. For saving to a new name, this can be done by using the shortcut Ctrl + x followed by Ctrl + w and entering the new filename.

Users can also open files in read-only format with the keys Ctrl + x followed by Ctrl + r and then entering the filename.

To insert the contents of another file inside your current file, hit Ctrl + x followed by i and enter the filename of the file whose contents are being added.

Whenever you open a file, the contents inside of the file are stored inside a buffer. If multiple files are opened, then multiple buffers exist. To see a list of buffers opened, hit the keys Ctrl + x followed by Ctrl + b.

We can also move from one buffer to another using the keys Ctrl + x followed by b.

Since buffers are another term for a file, they can also be killed by using the keys Ctrl + x followed by k. In this case, without entering anything and simply pressing enter would kill the default buffer. In the above below, that would be the scratch buffer.

To exit from Emacs, the shortcut Ctrl + x followed by Ctrl + c is defined. Emacs can also be suspended with the keys Ctrl + z.

 

Cursor Movement Shortcuts

For moving around the screen, Emacs specifies shortcuts for Cursor Movement as well, since these tend to be much faster than using a 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)

Editing Shortcuts

Emacs, as an editor, also provides users with a variety of shortcuts to work with. For repeating a command in the specified number of times, this can be done by hitting the keys Ctrl + u followed by entering the specified number the command will be repeated and then entering the command itself. For example, I want the cursor to move to the next line command to be repeated fifteen times. The image below shows this:

In Emacs, we first have to specify the beginning of selection on which we can apply commands like copy, cut, etc. This can be done by using the shortcut Ctrl + Space. Now, select the whole area by using your cursor.

For example, in the image below, the rectangle specifies the beginning of the selection and the circle its end.

Even the entire buffer can be selected by using the keys Ctrl + x followed by h.

Once your section of text has been selected, you can now copy this selected area by using the shortcut Alt + w. To kill or cut the text, Alt + d can be used to cut a single word, Ctrl + k can be used to cut a single particular line, and Ctrl + w can be used to cut the entire selected region.

The shortcut Ctrl + y is mainly used for pasting the region of text copied or killed. Since Emacs uses a kill-ring to keep track of all the killed text, it can paste old killed items by cycling through the kill-ring with the shortcut Alt + y.

Sometimes you hit the wrong keys and want to abort your command, or you specified the wrong command and want to end it. This can simply be done by using the shortcut Ctrl + g.

Undo and redo are also available in Emacs. Ctrl + / can be used to undo commands and Ctrl + g followed by Ctrl + / can be used to redo commands.

Ctrl + x followed by Ctrl + + (plus) is used to increase the text size of the screen and Ctrl + x followed by Ctrl + – (minus) is used to decrease the text size.

Search and Replace Shortcuts

Emacs allows you to perform an incremental search with the shortcut Ctrl + s. This command is used continuously to jump from one occurrence of the search to another.

An incremental search in reverse can also be done with Ctrl + r.

To search using regular expressions or regex in other terms, use the keys Ctrl + Alt + s.

Similar to the incremental search, a regex search in reverse can be done by Ctrl + Alt + r.

To perform a simple, replace operation, use the shortcut Alt + Shift + 5 (Alt + %).

Asking for the word to be replaced:

Asking for the word to replace with:

Similarly, a replacement operation using regular expressions can be done by using the keys Ctrl + Alt + Shift + 5 (Ctrl + Alt + %).

To go to a specified line number, the shortcut Alt + g followed by g is used. In this case, after hitting the shortcut, you have to enter the line number where you want to go to. For example:

Window Shortcuts

Emacs also allows you to open multiple windows inside the frame of your Emacs. To split a new window vertically, hit the keys Ctrl + x followed by 2.

Similarly, for splitting a new window horizontally, Ctrl + x followed by 3 is used.

You can also have both the vertical and horizontal windows opened.

To close a window, hit the keys Ctrl + x followed by 0. To only keep a single window on your frame, then Ctrl + x followed by 1 will be used.

Conclusion?

Despite having a pretty simple looking interface, a powerful core lies at the back of Emacs. It provides so many features to its users that can be easily accessible with shortcuts. This comes out to be pretty handy since now there is no need to go through the hassle of finding every one of these choices from the menu.

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.