How to Use Emacs: A Comprehensive Tutorial

Oodo Roland Uchenna
10 min readFeb 10, 2023

--

Are you constantly struggling to navigate through files while working on your Linux system? Or maybe you find yourself wasting precious time trying to remember complex commands and key combinations. Look no further than the powerful text editor, Linux Emacs.

In this guide, I will show you how Emacs can help solve these problems and make your work more efficient. From mastering window management to configuring advanced features, you will learn how to effectively use emacs to streamline your workflow and make editing a breeze.

Whether you are a beginner or a seasoned pro, this guide will take your Linux experience to the next level.

What is Linux Emacs and how is it different from other text editors?

Emacs is a software program that allows you to create, edit, and save text files. It is similar to other text editors like Microsoft word or notepad, but it has some additional features that make it more powerful and useful for programmers and advanced users.

For example, when you open a file with code in it. Emacs can highlight different parts of the code in different colors, which makes it easier to read and understand.

It also allows you to open multiple files at the same time, and easily switch between them.

Another example of its power is that it has a built-in terminal emulator that allows you to run command line commands directly from within the editor, so you don’t have to switch back and forth between the editor and the terminal.

To start using emacs, you can open it by typing “emacs” in the terminal or by searching for it in the applications menu if you are on windows or mac.

Once it is open, you can create a new file by pressing C-x C-f (CTRL + x CTRL + f) and type in the name of the file you want to create.

You can then start typing in the file and use the key bindings to navigate and edit the text.

Emacs also has a built-in tutorial that can be accessed by pressing C-h t (CTRL + h t), this tutorial will help you learn the basic commands and key bindings in emacs.

How to open and save files on Emacs

In Emacs, you can open and save files using key bindings.

To open a file in Emacs, you can use the key binding C-x C-f. This stands for CTRL + x CTRL + f.

After pressing the keys, you will be prompted to enter the name of the file you want to open.

For example, if you want to open a file called “myfile.c”, you would type “myfile.c” on the prompt and press the Enter key.

To save a file in Emacs, you can use the key binding C-x C-s. This stands for CTRL + x CTRL + s. After pressing these keys, the current file will be saved.

If you have not already saved the file, you will be prompted to enter a name for the file before it is saved.

For example, if you opened a new file called “myfile.c” and made some changes, you can save it by pressing C-x C-s keybindings.

It is important to know that in emacs, the key bindings are case-sensitive, so C-x C-f is different from c-x c-f, make sure to use the correct case when using the keybindings.

What is a buffer and how to switch from one to the other in Emacs

In the context of text editors like Emacs, a buffer refers to a temporary storage area for the text being edited. It is essentially a window into a file that allows you to make changes to the contents of the file.

When you open a file in emacs, a buffer is created for that file. Multiple buffers can be opened in emacs at the same time, allowing you to work on different files at the same time.

For example, you can have one buffer open for a file called “file1.txt” and another buffer open for “file2.txt”. You can switch between these buffers to edit the different files.

To switch between buffers in Emacs, you can use the command C-x b (CTRL + x, release both keys, and press b), which prompts you to enter the name of the buffer you want to switch to.

For example, if you have a buffer open for “file1.txt” and you want to switch to a buffer for “file2.txt”, you would press C-x b and then type “file2.txt” and press enter.

Another way to switch between buffers is by using the C-x left arrow and C-x right arrow keys. These keys allow you to switch to the previous and next buffer respectively.

You can also use the C-x C-b command to display a list of all open buffers and select the one you want to switch to by typing the buffer number or buffer name.

How to cut and paste lines and regions in Emacs

In Emacs, you can use keyboard shortcuts to cut and paste lines and regions of text.

To cut a line of text, place the cursor at the beginning of the line and press C-k. This will cut the entire line and store it in a special area called the “kill-ring.”

The kill-ring is like a clipboard, where you can store multiple items that you have cut or copied.

To cut a region of text, you first need to mark the beginning of the region by pressing C-@ or C-SPC. Then, move the cursor to the end of the region you want to cut.

Finally, press C-w.

This will cut the region and store it in the kill-ring.

Once you have cut or copied text, you can paste it by pressing C-y. This will paste the most recently cut or copied text at the cursor location.

You can also paste the previous text from the kill-ring by pressing M-y. For example, if you have cut or copied multiple items, you can use M-y to paste the second–most recent item, then M-y again to paste the third-most recent item, and so on.

Here is an example of how you might use these shortcuts to cut and paste a region of text:

  1. Open a file in Emacs and move the cursor to the beginning of the region you want to cut.
  2. Press C-@ or C-SPC to mark the beginning of the region.
  3. Move the cursor to the end of the region you want to cut.
  4. Press C-w to cut the region and store it in the kill-ring.
  5. Move the cursor to the location where you want to paste the text.
  6. Press C-y to paste the text.

Note that, you can also use C-x h to select all the lines in the buffer and then cut and paste as above.

How to search forward and backward

In Emacs, you can search for text both forward and backward using the keyboard shortcuts C-s (search forward) and C-r (search backward).

When you press C-s, the cursor will move to the next occurrence of the text you type. Pressing C-s again will move the cursor to the next occurrence of the text.

When you press C-r, the cursor will move to the previous occurrence of the text you typed. Pressing C-r again will move the cursor to the previous occurrence of the text.

Here is an example of how you might use these shortcuts to search for a specific word in a file:

  1. Open a file in Emacs.
  2. Press C-s to initiate a forward search.
  3. Type the word you want to search for, and press enter.
  4. The cursor will move to the next occurrence of the word in the file.
  5. Press C-s again to move the cursor to the next occurrence of the word.

Similarly, you can use C-r to search for a word backward.

In addition to this, you can also use C-s to search for a regular expression (regex) and M-x occur to display all matches of a regex in a buffer.

It is also possible to search and replace text in a buffer or a file using the command M-x replace-string.

How to undo in Emacs

In Emacs, you can undo changes to a file by using the undo command. There are a few different ways to invoke this command, depending on your preference and the situation:

  • The most common way to undo changes is to use the C-/ (CTRL + slash) key binding. Press and hold the Control key and then press the / key. This will undo the last change you made to the file.
  • Another way to undo changes is to use the C-x u (CTRL + x u) key binding. Press and hold the Control key, press x, release both keys, and then press u. this will also undo the last change you made to the file.
  • You can also use M-x undo command to undo the changes. Press M-x and type undo and press Enter.

You can also redo changes after undoing them using C-g key binding.

Here is an example of how you might use the undo command:

  1. Open a file in Emacs.
  2. Make some changes to the file, for example adding some text or deleting some text
  3. If you realize that you made a mistake, press C-/ or C-x u to undo the last change you made.
  4. If you decide that you actually wanted to keep the change you just undid, press C-g to redo the change.

It is worth noting that the undo command keeps track of multiple changes, so you can keep undoing changes until you reach the original state of the file.

If you want to undo multiple changes at once, you can use C-u followed by a number (e.g. C-u 2 C-/) to undo that many changes, or use M-x undo-tree-visualize to visualize the undo tree.

Also, you can save your work before making any changes by using C-x C-s command or M-x save-buffer command.

How to cancel half-entered commands in Emacs

In Emacs, you can cancel a half-entered command by using the C-g (Control + g) key binding.

Here is an example of how you might use the C-g command:

  1. Start entering a command in the mini buffer, for example, M-x find-file
  2. Realize that you made a mistake and want to cancel the command
  3. Press C-g to cancel the command and then return to the previous state.

You can also use C-g to stop a command that is running and return to the previous state.

It is important to note that C-g command can also be used to cancel a running command, or to cancel a command that is stuck or taking too long.

This can be useful in situations where you accidentally started a command that you don’t want to run or when the command is stuck and not responding.

Here is an example of how you might use the C-g command to stop a running command:

  1. Start a command that takes a long time to run, for example, M-x find-file on a large file
  2. Realize that you don’t want to run the command anymore
  3. Press C-g to stop the command and return to the previous state.

It is worth noting that C-g is a versatile command and can be used in many situations to cancel or interrupt running commands or actions.

How to quit Emacs

In Emacs, you can quit the editor by using the C-x C-c (CTRL + x CTRL + c) keybinding. This will close the current buffer and exit the editor.

Here is an example of how you might use the C-x C-c command:

  1. Open a file in Emacs by using the C-x C-f command
  2. Make any changes you want to the file
  3. Save your changes by using the C-x C-s command
  4. Press C-x C-c to close the current buffer and exit the editor

Alternatively, you can also quit emacs using the M-x kill-emacs command by typing M-x kill-emacs and pressing Enter.

It is worth noting that if you have unsaved changes in any buffers, Emacs will prompt you to save them before exiting. And also if you have multiple open buffers, using C-x C-c will close the current buffer and return to the buffer you were previously in.

It is important to note that quitting emacs will close all the open buffers and unsaved changes will be lost. So, it is always a good practice to save your work before quitting emacs.

Resources

Conclusion

In conclusion, the Linux Emacs text editor is a powerful tool that offers a wide range of features for editing and manipulating text.

With its intuitive interface and customizable settings, it is a great choice for both novice and advanced users. By learning to use its various commands and keybindings, you can become more productive and efficient in your text editing tasks.

Whether you’re writing code, composing a document, or working on any other type of text-based project, the Linux Emacs text editor is an excellent choice for getting the job done quickly and effectively. So, give it a try and you’ll see the difference it makes in your workflow.

If you have any questions or challenges related to the topic, please feel free to post them on the forum at Forums.

#emacs #linux #linuxadministrator

--

--