Oct 21, 2023 15 min read

How To Use the Emacs Editor on Linux

Use the emacs editor on linux with our step-by-step tutorial. It is renowned for its robust and powerful editing functions

Use the Emacs Editor on Linux
Table of Contents


Before we start talking about how to use emacs editor on linux,let's briefly understand-What is an Emacs Editor ?

Emacs is a powerful and extensible text editor highly customizable and widely used among Linux users and developers. It is renowned for its robust and powerful editing functions. It can be modified and expanded with many modes, allowing it to be used as an Integrated Development Environment (IDE) for programming languages like Python, Java, and C.

Emacs presents itself as a bridge between the user-friendly nano text editor and the powerful Vi text editor. While its menus, help files, and command-keys are comparable to those of nano, its advantages and features are similar to those of Vi.

This tutorial will provide an introduction to using the Emacs editor on Linux.


You'll need an Ubuntu 22.04 server configured with a non-root user who has sudo privileges and a firewall enabled if you want to follow this tutorial. You can configure this by following our Initial Server Setup with Ubuntu 22.04 guide.

Step 1 – Installing Emacs

Start by seeing if Emacs is already installed on your system:


When the editor launches if the program is installed, it will say hello by default. If not, you'll see the following output:

Command 'emacs' not found, but can be installed with:
sudo apt  install e3           # version 1:2.82+dfsg-2
sudo apt  install emacs-gtk    # version 1:27.1+1-3ubuntu5
sudo apt  install emacs-lucid  # version 1:27.1+1-3ubuntu5
sudo apt  install emacs-nox    # version 1:27.1+1-3ubuntu5
sudo apt  install jove         # version
See 'snap info emacs' for additional versions.

The following command should be used to install Emacs:

sudo apt install emacs

You are now prepared to continue on to the following step after installing Emacs on your machine.

Step 2 – Using the Interface

Enter the command emacs to launch Emacs in your terminal:


Emacs waits for you to begin typing before starting with an empty editing buffer. Emacs shows the following welcome message when launched without a specified file:

Welcome to GNU Emacs

Press the TAB key to drag the cursor to the link "Visit New File" and then press ENTER to begin a new file. To create a new file, you can alternatively hit CTRL+X, followed by CTRL+F.

Create New File

To begin text editing, type in a filename. The file myfile.txt is employed in the example below. You can give this file any name you choose. Press ENTER after typing in your file name to continue.

The file will be empty and ready for text entry:

Empty File

There is a menu at the top of the screen. There is a sizable editing area below the menu. This is referred to as the main buffer, where you can type text or see a file's contents.

A copy of the file is first loaded into memory and then displayed in the main editing window when Emacs edits an existing file on disc. This section of memory is known as a buffer. All edits you make in the editing area are applied to the buffer as you progress through the document; however, the original file on disc is left untouched. Sometimes Emacs will automatically save, but the changes aren't saved to the disc until you explicitly save the document. The same holds true for brand-new files. Up until you save it, the buffer is where all modifications are made. In Emacs, your perspective of the buffer serves as the primary editing space.

A highlighted bar of text appears near the bottom of the screen after the main buffer. The status bar or the mode line is what this is known as. Depending on the mode Emacs is in, this text will be shown differently. The status bar compromises:

  • Name of the current file
  • Current cursor location
  • Current editing mode

The file's status (-- for unmodified files, ** for unsaved modifications files, and %% for read-only files).

The status bar is followed by a single line of empty space, which marks the conclusion of the screen. It is displaying the text "(New File)" in this instance. This area is known as the mini buffer. You interact with Emacs mostly through the mini buffer because it is a command-driven programme. Here, Emacs asks for command inputs and displays the results.

Emacs' GUI-based version and text-based version both handle windows in distinct ways. Text-based apps like Emacs cannot physically pop out of a terminal or console session, in contrast to GUI-based applications, which can. Like having two frames in a browser, Emacs splits its main buffer into two pieces when it needs to open a new window. The primary buffer is displayed in the top half, while the new content is shown in the bottom half. When you consult Emacs' help documentation or instructional materials, for instance, a new window will open.

Accessing the Menus

Typically, the entire screen is taken up when Emacs launches. A menu bar at the top of the screen provides access to the majority of its features.

Text-based menus cannot be dropped down with a mouse click, in contrast to GUI-based programmes. In actuality, using a shortcut key to highlight and navigate the menus is not possible.

Press the F10 key to open the menus. This displays a list of keys to access the menu items in a new window that opens under the primary buffer. You will be prompted to enter the necessary key by the mini buffer. The new window's contents will alter as soon as you press that key, reflecting the higher level of options.

No matter where you are in the menus, use the ESC key three times to escape. By default, this dismisses the menu window and returns you to the main buffer.

The following are some choices you can select from the Tools menu:

  • Calendar
  • Simple calculator
  • Programmable calculator
  • Searching a directory
  • Encrypting and decrypting document
  • Send and read e-mails
  • Search files using grep
  • Spell checking
  • Running shell commands and compiling code
  • Version control
  • Compare and merge files
  • Games

Accessing Help & Tutorials

Both tutorials and an extensive help system are available for Emacs. You can either use the menu by pressing F10 and selecting Help with the RIGHT or LEFT arrow keys, or you can access it by entering CTRL+H followed by the appropriate key. To review FAQs, tutorials, news, and other topics, for instance, type CTRL+H and then one of the following keys:

  • To access an Emacs tutorial, type t.
  • For a FAQ, press CTRL+F.
  • To find out about known defects and issues, press CTRL+P.
  • To read the Emacs Manual, press CTRL+R.
  • To find more packages, press CTRL+E.

Step 3 – Using Command Keys

You can begin becoming familiar with Emacs' command keys once you are comfortable with the user interface. You can begin entering and issuing commands simultaneously when a file is open.

Two or three keys are frequently required for command functions. The CTRL key is the most frequently used, followed by the ALT or ESC keys. In the Emacs environment, CTRL is denoted by the letter "C" in short form. Emacs notes like C-x C-c indicate that you should first press CTRL+X and then CTRL+C. Similar to this, C-h t implies to simultaneously press CTRL+H, then release both keys and hit t.

In Emacs, the ALT and ESC keys are referred to as meta keys. Use the OPTION key on Apple computers in place of the ALT key. There is an EDIT key on some keyboards. Emacs utilizes multi-key functions with the meta key in a manner akin to the CTRL key. For instance, the notation M-x indicates that you should press ALT or OPTION and x simultaneously. Similarly, you could use ESC+X to accomplish the same command.

In Emacs, the ENTER key is displayed as RET, which is short for return. Often, the ESC key is displayed as E.

It is possible to cancel a command or prompt by pressing the ESC key. For instance, you can repeatedly hit ESC to leave a particular menu. By hitting CTRL+G, an action can also be cancelled.

Saving and Quitting

You can save a document after making changes to it or adding text by pressing CTRL+X, followed by CTRL+S. The following message will be displayed by the micro buffer:

Wrote /home/sammy/myfile.txt

Press CTRL+X, followed by CTRL+C, to leave Emacs.

You'll get this notice if it didn't manually save the file before leaving:

Save file /home/sammy/myfile.txt? (y, n, !, ., q, C-r, C-f, d or C-h)

To save the file, press Y.

If you choose to press N for "no," you will see the following message:

Modified buffers exist; exit anyway? (yes or no)

If you want to leave without saving, type yes.

A lengthy document or assistance topic can be difficult to navigate. Fortunately, there are several ways to navigate a file with Emacs.

The following is a list of some typical navigational features:

To perform this function Use these keys
Moving to the next line CTRL+N (N for Next)
Moving to the previous line CTRL+P (P for Previous)
Moving one character forward CTRL+F (F for Forward)
Moving one character backward CTRL+B (B for Backward)
Moving one word forward META+F (F for Forward)
Moving one word backward META+B (B for Backward)
Moving to the start of a line CTRL+A
Moving to the end of a line CTRL+E (E for End)
Moving to the start of a sentence META+A
Moving to the end of a sentence META+E (E for End)
Moving one page down CTRL+V (or PgDn)
Moving one page up META+V (or PgUp)
Moving to the beginning of the file META+< (Alt + Shift + “<”)
Moving to the end of the file META+> (Alt + Shift + “>”)

Keep in mind that META indicates that you may press ALT, ESC, OPTION, or EDIT.

Step 4 – Editing Functions

Emacs allows you to choose or highlight a particular piece of a text file, which is a more specialized activity frequently required by word processors.

Marking Text Regions

To mark a text region, take the following actions:

  • Place the cursor where you want the selection to, begin by moving the mouse. Use any of the ways explained previously to move the cursor.
  • In order to set a mark to start your text highlighting, press CTRL+SPACEBAR or CTRL+@. Mark Activated will be displayed as the status message in the micro buffer.
  • You can finish the region by moving the cursor to that spot. By using any of the previously mentioned key combinations.
  • The text up to where your cursor is situated will be highlighted.
  • To remove the highlight from the highlighted text, press CTRL-SPACEBAR or CTRL+@ twice. Mark Deactivated will be displayed as the status message in the micro buffer.

As an alternative, you can use the UP and DOWN arrows on your keyboard to move your cursor while holding the SHIFT key, just as in a word processor.

Press META+H to choose the paragraph that your cursor is currently on. The following paragraphs in your text file will be selected if you keep pressing META+h.

Press CTRL+X then h to select everything in the main buffer (i.e., "select all").

Cutting, Copying, and Pasting Text

You can copy, cut, and paste text in a manner akin to a word processor:

  • Press META+W to copy the text you've chosen.
  • Press CTRL-W to trim the text selection.
  • Press CTRL-Y to paste a selection of text.

Deleting Text

The Backspace and Delete keys function as expected when it comes to deleting text.

Move the cursor to the beginning of a word and hit META+D to swiftly erase the entire word. Hold down the META key while pressing D repeatedly to remove numerous words. One by one, the words will be removed.

Place the cursor where you want it to go, then hit CTRL+K to erase the entire line. With this, the text is removed all the way to the end of the line.

META+K can be used to remove a sentence. Please be aware, though, that if there aren't two spaces following the full stop, Emacs will remove the entire line or more. Emacs recognizes when a sentence has broken across several lines when there are two spaces following a full stop.

Undoing and Redoing

By pressing CTRL+X and then u, the previous action can be reversed. CTRL+_ is an alternative key combination that would require pressing CTRL, SHIFT, and - to make an underscore.

Press CTRL+G and then CTRL+_ to undo your most recent undo.

Searching and Replacing Text

Emacs supports both forward and backward searching. When using forward search, the term you specify will be looked up from where the cursor is currently located. For a backward search, the order is reversed.

  • CTRL+S is the forward search shortcut. Enter the desired text into the mini-buffer prompt after that.
  • To search in the past, press CTRL+R.

As soon as you enter a search word, Emacs will look for matches and highlight them in the main buffer.

In a text file, for instance, a search for the word "cat" will highlight each occurrence in the main buffer like follows:

Text Displayed

Use these steps to replace text:

  • Click META+%. The text to be searched with Query replace: will be requested by the mini buffer.
  • Press ENTER after entering the replacement text.
  • You can see the Query replace your_search_term with: in the tiny buffer.
  • Press ENTER after entering the word or phrase you want to use in place of the your_search_term.
  • Each match will be highlighted, and a popup asking you to make a change will appear. The mini buffer will request Query replacing your_search_word with your_replacement_word: (C-h for help).
  • To change the current match found, use the y key.
  • To move on to the next game, press n.
  • To leave without any substitutes, press q.
  • To perform a global replacement without any prompts, press the ! key. This information will be output by the micro buffer: replaced number occurrences.

Adding Left, Right and Center Alignment

Move the cursor to the start of the line you want to centre, then press META+O and META+S.

Do the following to justify a chosen text region:

  • Highlight the text you want to justify.
  • Click META+X. A answer will be awaited by the micro buffer.
  • Press the TAB key after entering set-justifiction-.
  • set-justification-center, set-justification-left, set-justification-right, set-justification-none and set-justification-full. are the completion options that are available to you.
  • Select set-justification-right or another option to complete the justification command, then press ENTER.
  • The chosen text will be justified in the manner you specify.
Selected Text

Converting Case

Here is an illustration of the text that corresponds to the various justification settings:

To perform this function Use these keys
Capitalizing a word after the cursor META+C (C for capitalize)
Converting a word to lowercase META+L (L for lowercase)
Converting a word to uppercase META+U (U for uppercase)
Converting a paragraph to uppercase Block select, then CTRL+X CTRL+U
Converting a paragraph to lowercase Block select, then CTRL+X CTRL+L

Several distinct commands can be used to convert case. The following list of command keys:

You have typed C-x C-l, invoking disabled command downcase-region. It is disabled because new users often find it confusing. Here’s the first part of its description: Convert the region to lowercaselower case. In programs, it wants two arguments.These arguments specify the starting and ending character numbers of the region to operate on.  When used as a command, the text between point and the mark is operated on. Do you want to use this command anyway? You can now type 'y' to try it and enable it (no questions if you use it again). 'n' to cancel--don’t try the command, and it remains disabled. 'SPC' to try the command just this once, but leave it disabled. '!' to try it, and enable all disabled commands for this session only.
List of Commands

Press the aforementioned keys to continue.

Managing Windows

Emacs' window management features can make working with your files more productive.

For instance, press CTRL+h then t to enter the Emacs tutorial from your main buffer. The Emacs tutorial is now displayed in your main buffer window. Press CTRL+X followed by b to return to the myfile.txt buffer. The switch buffer command looks like this. You will be asked for a buffer name to switch into by Emacs. Enter after you've entered the buffer's name, myfile.txt. You will then be sent to the file you specified from the Emacs tutorial.

Step 5 – Entering Modes

Entering a Major Mode

The fact that Emacs may assume several modes is one of the reasons it has been so widely used in the UNIX community. A setting can improve functionality of Emacs.

Depending on the mode selected, Emacs can be used as a word processor for writing text files, or it can be customized for complex activities like writing Python, C, or Java code. You can alter Emacs' mode, for instance, to make it compatible with version control systems, execute shell commands, or read man pages.

Emacs modes come in two main types. The principal mode is the first one. Emacs can be used as an integrated development environment (IDE) for scripting or programming languages while in major mode. In this mode, the programme offers specialized features including colour syntax highlighting, formatting, indentation, menu options for certain languages, and automatic debugging and compiler interfaces.

You can use Emacs to create a "Hello World" app in Python as an example.

Enter the following commands in your terminal's root directory:

cd ~
emacs hello.py

The file extension is recognized by Emacs, which launches in Python mode. Enter the next line of Python code in the main buffer:

print "hello world!\n"

The keywords are now highlighted in colour in the syntax. Also take note of the fact that the mode you're now in is displayed on the status line above the mini buffer. Additionally, there is a unique entry just for Python in the main menu:

Keywords Highlighted

Use CTRL+X CTRL+S to save the buffer.

Press META+X to switch the major mode in Emacs. The mini buffer will hold on while you reply. After that, you can switch to another mode. Major mode examples include the following:

  • c-mode
  • python-mode
  • java-mode
  • html-mode
  • perl-mode
  • shell-script-mode
  • text-mode

Entering a Minor Mode

Minor modes give more specific characteristics as compared to major modes. These features may be connected to a particular major mode or may affect the entire system, regardless of the major mode. Furthermore, unlike major modes, many minor modes may be active simultaneously. Minor modes operate like switches, with some being on by default and others not. Calling a minor mode will turn it off if it is already active. It will be turned back on if it is currently off.

The option for establishing justification utilized in the aforementioned instances is an illustration of a minor mode.

The auto-fill-mode is another illustration of a minor mode. Press the META+X key in your Emacs editor, then select auto-fill-mode  to access this mode.

When a line of text exceeds 70 characters, this setting allows it to break and wrap to the following line. Keep in mind that when you activate a minor mode, it functions very similarly to a toggle switch. The line wrap can be turned off by using the same command again.

More illustrations of minor modes are provided here:

  • auto-save-mode: This turns on the automatic saving feature, which records the main buffer's contents on a recurring basis in the background.
  • line-number-mode: This turns on or off the status bar's display of the current line number.
  • linum-mode: Turns on or off the line numbers that are displayed along the window's left edge.
  • column-number-mode: Displays the cursor's current location in the status bar.
  • overwrite-mode: This is comparable to hitting the INS key on a keyboard. When activated, it will replace any text that is currently being typed to the right of the cursor.
  • menu-bar-mode: This can turn on or off the main menu.

FAQs to Use Emacs Editor on Linux

How do I install Emacs on Linux? 

You can install Emacs on Linux by using your distribution's package manager. For example, on Ubuntu, you can use the command sudo apt-get install emacs to install it.

What is the Emacs command mode? 

Emacs has two main modes: command mode and insert mode. In the command mode, you can execute various commands and navigate within the editor.

How do I switch to insert mode in Emacs? 

In Emacs, you start in command mode, and you can switch to insert mode by pressing the i key. In insert mode, you can start entering and editing text.

How can I customize Emacs? 

Emacs allows extensive customization through its configuration file called .emacs or .emacs.d/init.el. You can add or modify settings, keybindings, and install additional packages to suit your preferences.

Can Emacs be used for programming?

Yes, Emacs is widely used for programming. It supports syntax highlighting, code completion, code navigation, and integration with various programming languages and tools.

What are some useful features of Emacs for developers? 

Emacs offers numerous features for developers, such as built-in version control integration, terminal emulation, project management, debugging capabilities, and support for various programming modes and frameworks.

Are there Emacs extensions and packages available? 

Yes, Emacs has a vast collection of extensions and packages that can be installed to enhance its functionality. You can browse and install packages from the Emacs package repository or use package managers like use-package to simplify the process.


Emacs is a versatile and powerful text editor with a wide range of features and customizability. Understanding basic Emacs commands, modes, and customization options can greatly enhance your productivity. Whether you're editing text files or writing code, Emacs provides ample tools and extensions to support your workflow.

You now know about the various commands, editing features, and modes in Emacs. If you have any queries or doubts, please leave them in the comment below.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.