How to Install and Use Emacs in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

Emacs is one of the oldest text editors with ongoing development, providing rich and powerful features that can be further extended using plugins.

It provides a wide range of functionalities that can be very useful for programmers and Linux administrators while modifying the configuration file.

Tutorial Details

DescriptionEmacs
Difficulty LevelModerate
Root or Sudo PrivilegesMaybe
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesemacs
Internet RequiredYes (for installation)

How to Install Emacs in Linux

Yes, I agree with you that this tool is aged and should be included in Linux distributions.

Still, it was not included; however, it is available in major Linux repositories, from which you can install it using the following command.

$ sudo apt install emacs -y                                        #For Debian/Ubuntu/Pop!_OS
$ sudo dnf install emacs -y                                        #For Fedora/AlmaLinux/Red Hat
$ sudo pacman -Sy emacs                                         #For Arch/Manjaro/EndeavourOS

Getting started

If you directly execute the “emacs” command, it will open the GUI version of this application.

$ emacs

Output:

Emacs GUI window

However, today you will learn mostly on the command line. So, execute the following command to open the command-line version of the Emacs editor.

$ emacs -nw

Output:

Opening the emacs command-line

From here, you can navigate using the arrow key. Apart from that, if you want to create a new file, navigate to the “Visit New File” link using the arrow keys and press the “enter” key.

Creating or opening a new file in Emacs

It will ask for the existing file location from the directory, which you can easily navigate using the Tab and arrow keys.

However, I will create a new file (ex: “file.txt”) that does not exist in my home directory and press the “enter” key to open a new file.

Creating a new file in Emacs

From the above window, if you changed your mind and want to open an existing file, press “Ctrl+x” and then the “Ctrl+f” shortcut key simultaneously.

Searching for files in the new file window in Emacs

Above, you can specify the full file path or press the “enter” key at the current path “~/” to open the emacs file manager and navigate through arrow keys to open the file from your system.

Emacs built-in file manager

However, I stick with my previously created file (ex: “file.txt”) and started adding new content.

Adding content to new file in emacs editor

Currently, your file is automatically saved in your home directory in “#yourfilename#” file format (ex: “#file.txt#“).

If you are done with editing and want to save the file, press “Ctrl+x” and then “Ctrl+s” simultaneously.

Saving a file in the Emacs editor

To close the editing window referring to the emacs editor, press “Ctrl+x” and then the “Ctrl+c” shortcut keys simultaneously.

In the future, if you want to open the same file, specify the path of that file to the “emacs” command as shown.

$ emacs file.txt -nw

Modify the file and save with “Ctrl+x” and then the “Ctrl+s” shortcut key, and press “Ctrl+x” and then “Ctrl+c” to quit the Emacs editor.

You might be confused with this manner of editing files. However, you can watch the below video to understand it more easily.

Also, don’t forget to take a look at the following cheat sheet.

Emacs Cheat Sheet

  • C = Control Key
  • M = Meta Key (Alt on PCs, Option on Macs)

Basics

KeysDescription
C-x C-cQuit Emacs.
C-gCancel any in progress keybinding/shortcut command.
C-x C-sSave current buffer.
C-x bSwitch to a different buffer.
C-x kKill/close current buffer.
C-x C-fOpen a file.
C-/Undo the last action.

Buffer/File

KeysDescription
M-<Move to beginning of buffer/file.
M->Move to end of buffer/file.
C-vMove one screen forward.
M-vMove one screen backward.
C-lCenter screen on position at cursor.

Lines

KeysDescription
C-aMove to beginning of the line.
C-eMove to end of line.
C-nMove to the next line.
C-pMove to the previous line.
M-g gGo to line.

Words

KeysDescription
M-fMove forward one word.
M-bMove backward one word.

Characters

KeysDescription
C-fMove forward one character.
C-bMove backward one character.

Searching

KeysDescription
C-sRegex search for text in the current buffer and move to it. Press C-s again to move to next match.
C-rSame as C-s, but search in reverse.
M-%Search and replace.

Kill Ring (Copy/Cut/Paste)

KeysDescription
C-spacebarCreate a region (select). Use movement keys to expand the region selected. Then use copy/cut commands below.
M-wCopy region to kill ring (copy).
C-wKill region (cut).
C-yYank (paste).
M-yCycle through kill ring after yanking/pasting. This is like a copy/paste clipboard.
M-dKill/delete word starting at cursor (cut).
C-kKill/delete line starting at cursor (cut).

Editing

KeysDescription
TabIndent line.
C-jNew line and indent, equivalent to enter followed by tab.
M-\Delete all spaces and tabs around point.
C-dDelete the next character.
M-backspaceDelete previous word.
C-x space (use movement keys to select up/down).
C-x r t (type key to insert, then Enter)
Rectangle mode

Formatting Tricks

KeysDescription
C-tSwap two characters at the point/cursor.
M-uChange the word at the point/cursor to uppercase.
M-lChange the word at the point/cursor to lowercase.
C-u n charInsert ‘n’ (a number) copies of ‘char’ (a character).

Help

KeysDescription
C-h k key-bindingDescribe the function bound to the key binding. To get this to work, you actually perform the key sequence after typing C-h k.
C-h fDescribe function.

That’s all for now. If you have anything else that needs to be added to this article, please let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.