Emacs Editor Tutorial – An Absolute Beginners Reference

Emacs editor is one of the oldest and most versatile text editors available for Linux UNIX-based systems. It has been around for over 20 years. It is known for its powerful and rich editing features.

Emacs offers amazing editing features like that of Vim editor and is as user-friendly as the nano editor.

You won’t have to enter insertion mode to edit a file. It resembles the nano editor in the way that whatever you type will directly be written to the buffer. Emacs’ controls are easy to remember and the editor offers comprehensive documentation as well.

First version of Emacs was implemented in 1976 on the MIT AI Lab’s Incompatible Timesharing System (ITS), as a collection of TECO macros.

The name “Emacs” was is an abbreviation for “Editor MACroS”. The version of Emacs we are using is actually GNU Emacs. It was originally written in 1984 by Richard Stallman.

Emacs Editor from a Beginner’s Perspective

In this tutorial we will cover the basic editing commands you need to know to get started with Emacs.

Emacs
Emacs

1. Installing emacs on your system

To check whether you have emacs installed on your system run :

emacs

If you don’t have emacs editor on your system, you can install it by running the following apt command.

sudo apt install emacs

During installation you will be prompted to confirm the installation. Press Y to continue.

To check the version post installation use the command :

emacs --version 
Version 1

Once you have emacs on your system, you can type emacs in the terminal to get started.

emacs
Emacs Screen
Emacs Screen

You can get help using Ctrl + h.

While reading the help section remember that ‘C-’ means the CTRL key. ‘M-’ means the Meta (or Alt) key.

Let’s create our first file using emacs.

2. Create a new file in emacs

To create a new file using emacs use emacs with the filename and press enter.

emacs [filename]
Emacs New File Edit
Emacs New File Edit

This is what the screen will look like. This is called the buffer and this is where you can write your text. Let’s try writing some text on to the buffer.

text

After you are done editing, you will see the following text appear at the bottom.

Auto saving

Emacs has an auto saving feature that automatically saves the buffer. We will see what this means shortly.

3. Save and Exit

Once you are done editing your file, you would want to save your changes. The shortcut to save your changes and exit the emacs editor is Cntrl + X followed by Cntrl + C. If you only want to save the changes without quitting, you can type in Ctrl + X – Ctrl + S

# Exit with a prompt to save unsaved changes
Cntrl + X - Cntrl + C

# Save without exiting
Ctrl + X - Ctrl + S

Check if the changes were saved to the file using the cat command :

cat [filename]

4. Restoring Autosaved Data in Emacs Editor

You can exit without saving your changes to the file by using Cntrl + X followed by Cntrl + C.

Cntrl + X - Cntrl + C

When you do this, the bottom of the screen will prompt the following :

Exit Emacs
Exit Emac

Press ‘n‘ and enter to exit without saving.

Now when you will open the same file again in emacs editor you will see the following message.

Recover Data
Recover Data

This is what autosave means in Emacs. Emacs saved your buffer from the last time you were editing and it gives you the option to recover the data.

The M in the M-x stands for <META> key, which depending on your operating system will be different. For Mac, it’s the Command key, while in Windows it’s the ALT key.

To recover the data press ALT + x followed by the following command :

recover-this-file

Press Enter.

Now Emacs will show you the following screen. Type ‘yes’ and press Enter to restore.

Edit Version

And…. Voila, your text is back.

Recover Text

5. Navigating the Emacs Editor Interface

The shortcuts to navigate around in the emacs editor are:

  • ctrl-a : Beginning of the line.
  • ctrl-e : End of line.
  • ctrl-n : Move the cursor to the next line.
  • ctrl-p : Move the cursor to the previous line.
  • ctrl-f : Move forward by one character.
  • ctrl-b : Move back by one character.
  • ESC > : End of the buffer.
  • ESC < : Starting of the buffer.
  • ESC f : Move forward by one word.
  • ESC b : Move back by one word.

Alternatively you can also use the arrow keys to move around.

6. Search Text in Emacs

To search for text in your file you can use Emacs’ search command. To activate the search press Ctrl + S.

The bottom of the editor will look like :

Emacs Search
Emacs Search

You can enter the string you want to search for. Emacs editor will highlight all the instances of your string in the buffer.

Linux Search
Linux Search 1

Another advantage of Emacs is that the search results are highlighted in real time.

For example, as you start typing, the editor will start highlighting the instances of your search.

Linux Search

7. Highlighting Text

To highlight text in your file you can press Ctrl + Space.

This shortcut sets the mark and you can use navigation shortcuts to start highlighting.

Mark Set
Highlight Text
Highlight Text

To deactivate the mark press Ctrl + Space again.

8. Cut, Copy & Paste

To cut or copy a portion of your text, first highlight the text.

After highlighting use the following shortcuts to cut/copy.

  • Cut: Ctrl-w
  • Copy: ESC-w

To paste the text use the shortcut Ctrl + Y.

Copy And Paste
Copy And Paste

Conclusion

This tutorial was about editing text on Emacs editor. You should also try out Nano editor. Read this tutorial to get started with nano editor.