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
Description | Emacs |
Difficulty Level | Moderate |
Root or Sudo Privileges | Maybe |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | emacs |
Internet Required | Yes (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:
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:
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.
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.
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.
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.
However, I stick with my previously created file (ex: “file.txt”) and started adding new content.
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.
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)
Keys | Description |
---|---|
C-x C-c | Quit Emacs. |
C-g | Cancel any in progress keybinding/shortcut command. |
C-x C-s | Save current buffer. |
C-x b | Switch to a different buffer. |
C-x k | Kill/close current buffer. |
C-x C-f | Open a file. |
C-/ | Undo the last action. |
Keys | Description |
---|---|
M-< | Move to beginning of buffer/file. |
M-> | Move to end of buffer/file. |
C-v | Move one screen forward. |
M-v | Move one screen backward. |
C-l | Center screen on position at cursor. |
Keys | Description |
---|---|
C-a | Move to beginning of the line. |
C-e | Move to end of line. |
C-n | Move to the next line. |
C-p | Move to the previous line. |
M-g g | Go to line. |
Keys | Description |
---|---|
M-f | Move forward one word. |
M-b | Move backward one word. |
Characters
Keys | Description |
---|---|
C-f | Move forward one character. |
C-b | Move backward one character. |
Keys | Description |
---|---|
C-s | Regex search for text in the current buffer and move to it. Press C-s again to move to next match. |
C-r | Same as C-s, but search in reverse. |
M-% | Search and replace. |
Kill Ring (Copy/Cut/Paste)
Keys | Description |
---|---|
C-spacebar | Create a region (select). Use movement keys to expand the region selected. Then use copy/cut commands below. |
M-w | Copy region to kill ring (copy). |
C-w | Kill region (cut). |
C-y | Yank (paste). |
M-y | Cycle through kill ring after yanking/pasting. This is like a copy/paste clipboard. |
M-d | Kill/delete word starting at cursor (cut). |
C-k | Kill/delete line starting at cursor (cut). |
Keys | Description |
---|---|
Tab | Indent line. |
C-j | New line and indent, equivalent to enter followed by tab. |
M-\ | Delete all spaces and tabs around point. |
C-d | Delete the next character. |
M-backspace | Delete previous word. |
C-x space (use movement keys to select up/down). C-x r t (type key to insert, then Enter) | Rectangle mode |
Keys | Description |
---|---|
C-t | Swap two characters at the point/cursor. |
M-u | Change the word at the point/cursor to uppercase. |
M-l | Change the word at the point/cursor to lowercase. |
C-u n char | Insert ‘n’ (a number) copies of ‘char’ (a character). |
Keys | Description |
---|---|
C-h k key-binding | Describe 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 f | Describe 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.