There are many text-based editors in Linux. Some come with most distros, others you have to install after the fact. Text-based editors are an important tool for any Linux user or admins. Servers often don’t have a GUI, and while Linux in itself is very stable, I have seen GUI crash many times. When you lose your GUI, having a set of text-based tools that you are comfortable with is a must.
Before I get started on the basics of operating GNU Emacs, I want to clarify something first. You are probably aware of the “Emacs Vs Vim” war that is responsible for many over-heated discussions. I love Vim, used it for over a decade. But here is the thing about Emacs, it is not just a text editor.
In its core, Emacs could simply be described as a framework of buffers and frames. Frames are how you split your windows, you can have as many frames as you want. In GUI mode, you can have multiple Emacs window, each containing one or more frames. Then you have the buffers, buffers are filled with contents being fed from somewhere. When you feed a buffer with a file, then Emacs plays the role of a text editor. You can even use Emacs as your window manager.
Get familiar with Emacs layout
First, let’s focus on the basics. Here you will learn the basics of operating Emacs.
Emacs can be installed directly through your distribution’s package management. After installation, starting Emacs will start the GUI mode. This mode can be useful when you start, as it provide a menu bar access. It is important to remember that every menu entry simply execute an Emacs command and this can all be done in text mode. To force text mode while in a GUI environment, run emacs –nw in a terminal.
When you first start Emacs, you will be presented with a welcome screen. This screen already displays more of Emacs features. The underlined text are links, they can be activated with a mouse click or press enter with the cursor on one. Some are links to a webpage, which would more than likely open eww (Emacs built-in web browser). There is also a built-in tutorial on that page.
Your initial layout will consist of a single frame, more than likely containing the content of about Emacs. Below the frame, you have the status bar and what appears to be an empty space below the status bar. This empty space is actually a mini-buffer that Emacs uses to interact with you. In the image, the mini-buffer has the text <Print> is undefined.
The essential concept of key bindings in Emacs
Key binding (or keyboard shortcuts) is how you command Emacs. It always starts with a key binding.
Emacs uses modifier keys as the key binding prefix. Most important modifiers are C (Ctrl), M (Meta), S (Shift). M (Meta, usually assigned to ALT).
To summarize, the key convention is:
- C = Ctrl
- M = Meta = Alt
- S = Shift
I honestly think that the key binding is one of the main reason people move away from learning Emacs. There are over 1300 key bindings in a default Emacs setup. But don’t forget, Emacs is only a text editor when editing files, those 1300+ key bindings do a lot more than edit files.
So how does one start learning Emacs and its obsession with key bindings? It is fairly simple, the basic idea is to practice a few key binding at a time, they will become muscle memory very quickly. When I record video lessons about Emacs, I say the key binding as I press them, well at least I try. The truth is that the keystroke are long done and processed while I am still trying to figure out which ones they were. The biggest and hardest part is this document.
Let’s get started and remember to practice the key binding a lot!
The best way to explain how we write key binding is with a few example. Not all the examples are functional key binding though:
- C-x = CTRL+x
- C-x 2 = Press CTRL and x then press 2 (CTRL key has to be released before pressing 2)
- C-x C-2 = Press CTRL and x then CTRL and 2. Or CTRL-x-2 (Don’t release CTRL)
- M-x command <RET> = Press META+x
You will also regularly see in documentation the key binding written with text in ().
C-x C-f (find-file)
The text inside () represent the Emacs function that will be executed with this key binding. It will become clear why this is important soon.
Emacs favorite key are CTRL and ALT. While the ALT key does not pose a problem, extensive use of the Left CTRL key is very well known to cause pinky finger problems. You can easily use xmodmap to swap CTRL and CAPSLOCK key or any other key you prefer.
Using Emacs with key bindings aka Emacs Commands
As previously mentioned, focus on learning a few key binding at a time, they will become muscle memory. The first set is the hardest part but will be enough for you to start working with Emacs as a text editor.
|Open and save files|
|Search & replace|
|Select, cut, copy & paste|
|C-< SPACE >||set-mark-command|
Let’s talk about these in details.
Manipulating Frames in Emacs
As previously mentioned, frames are how Emacs splits its window. Before learning about buffer let look at how we split our screen.
- C-x 2 will split the current frame horizontally.
- C-x 3 will split the current frame vertically.
- C-x o will move focus to the next frame. If the mini-buffer is active, it will be part of the cycle.
- C-x 1 will close all other frame, leaving only the current frame. It does not close buffers.
- C-x 0 close the current frame.
The image below displays many frames, some displaying the same buffer but with a cursor at different location.
Manipulating Buffers in Emacs
- C-x b (switch-to-buffer)
- C-x C-B (list-buffers)
- C-x k (kill-buffer)
Buffers are what you work with, buffers contain data, mostly text. When you open a file, the content of that file is placed in a buffer. This buffer will be named after the filename. You then work within the buffer. Changes are applied to the source (file) when saving is requested. By default Emacs has autosave. It does not save every x minutes, but every x modifications done to the buffer. An open buffer with no modification will not trigger the autosave.
To switch to a different buffer, press C-x b, the minibuffer will activate and is ready to receive the buffer name. Autocompletion is bound to <TAB> key. If multiple matches are possible, press <TAB> a second time will create a temporary buffer with all possible match. Should you provide a nonexisting buffer name, a new buffer will be created. This does NOT create a new file. I often use this to create temp buffer.
It takes no time to have many buffers, and you may not remember all of them. To list all the currently opened buffers, press C-x C-b. A new buffer will open in a split frame with the full list of buffers. You can maneuver within that buffer with the arrow key and switch to buffer at point with <RET>.
From within the buffer list, you can flag the entry at point for:
- saving by pressing s
- Deletion (Kill the buffer, does not delete the file) by press d or k
- Remove flags by pressing u
- Execute all marked flags by pressing x
To kill any buffer, press C-x k the mini buffer will activate and wait for a buffer name, you can enter any existing buffer name and press <RET>. Should you not provide a name and press <RET>, Emacs will close the current buffer.
The following image display the buffer list with buffers flagged for saving or deletion.
Open and save files in Emacs
- C-x C-f (find-file)
- C-x C-s (save-buffer)
Most of the time you need to work with files. Start by opening a file by pressing C-x C-f. The mini buffer present you with a path and is ready for you to enter the path for the file you want to open. Auto-completion works here too, <TAB> will autocomplete directory and file names. Once you are satisfied with the file path, press <RET>. A new buffer will open with the content of the file. The buffer will be named after the file. Once a buffer has modification, 2 stars will appear on the left side of the status bar. To save your modification: C-x C-s
If you provide a path to a nonexisting file after pressing C-x C-f, a new buffer will be created but the file will be created only when you save the buffer.
Should you provide a directory path after C-x C-f, a new buffer will be open with the content of the directory. This new buffer will be in DIRED mode. While in DIRED mode you can maneuver with the arrow keys. When on the desired file, press <RET> and it will be open in a new buffer. Should you again choose to press <RET> on a directory, once again a new buffer will be open in DIRED mode.
Many operations can be done to the filesystem while in DIRED mode, but as previously explained, let’s focus on the basics first.
When you work on a Linux system you often have to work on files that require root access. Emacs handles this very well. Do not close Emacs and run it as root, this is not a good idea.
Press C-x C-f then proceed to erase the given path in the minibuffer and replace it with /sudo::/path/to/file <RET>. Emacs will attempt to open the file with the sudo command and if required you will be prompt for your password.
Should you attempt to use auto-completion <TAB>, your password will be requested if needed and auto-completion will work. DIRED mode can be opened as root. Note that a new buffer named *tramp/sudo …. will be created. This is a buffer needed by Emacs to handle sudo.
Should you remove it, although not recommended, you will more than likely be asked for your password when attempting to save and the tramp buffer will be back. Opening multiple buffers as root will result in multiple tramp buffers.
It is as easy to open a file on a remote computer using ssh from within Emacs:
C-x C-f /ssh:[email protected]:/path/to/file
Should you be wondering about opening a file on a remote system as root? Yes, but I will keep this one for another time.
This image show on the left frame DIRED mode of my home directory and on the right my .emacs file.
Search & Replace in Emacs
- C-s (isearch-forward)
- C-r (isearch-backward)
- M-% (query-replace)
To perform a forward search, press C-s this will activate the minibuffer for you to enter the text to search. The search happens as you type. To move to the next match, press C-s again. The search will wrap around but after warning you once. Note that pressing backspace will first go back in the previous match before deleting a character.
To search backwards, press C-r
To replace, press M-% and you will then have the minibuffer ready to take the search parameter. Enter it and press <RET>, then proceed to provide the replacement string and press <RET>.
The search & replace will execute from cursor to end of buffer.
Select, cut, copy and paste in Emacs
- C-<SPACE> (set-mark-command)
- C-w (kill-region)
- M-w (kill-ring-save)
- C-y (yank)
- M-y (yank-pop)
Emacs has a whole different concept of select, cut, copy & paste. First, let’s look at selecting. While the mouse works very well in GUI mode, learning to not use it will pay off when GUI is gone. What you call a selected area, Emacs called an active region. To make a selection or activate a region, place your cursor at the beginning of the desired area, then press C-<SPACE> move your cursor 1 character passed the end of the desired area. This region is automatically activated. While this active region concept may seem pointless now, it is important to understand it for when you start making function to automate repetitive tasks.
Emacs does not cut, it kills. When you kill an active region with C-w, Emacs will “cut” it out of the buffer and save it in a kill ring. The kill ring keeps multiple entries either killed C-w or copied M-w. You can then yank (paste) those entries out of the kill ring and into the current buffer at the point with C-y. If right after pressing C-y you proceed with M-y, the entry that was paste into the buffer from the kill ring will be replaced with the previous one. Yanking an entry from the kill ring does NOT remove it from the kill ring and can be yanked again later.
Execute extended command in Emacs
- M-x (execute-extended-command)
Meta-x is a unique key binding. After pressing the key binding, look at the mini buffer. Your cursor will automatically be placed there and Emacs and is ready to receive a command.
The commands for the key bindings are written in parentheses. Do not type the parentheses when typing the command. After writing your command, press <RET>.
It is common to see M-x command written in the following format:
M-x find-file <RET> ~/path/to/file <RET>
Note that auto-completion is supported with the key <TAB>
Why learn about the command when I can use key binding? First, even with over 1300 key bindings, not every command has one. Commands also this gives you the power to write functions to perform the desired modifications to a selected area (active region) or entire buffer. Those functions can then be called with M-x functionName and if needed bound to a key combination of your choice.
One command worth showing to a new user is M-x shell <RET>. Remember as I said Emacs is not just a text editor. The above command will open a new buffer with a shell prompt. This is not a full terminal emulator, for example, it does not handle curl based application very well. The display is not refreshed properly. But it has other advantages. This shell buffer is a read/write text buffer. When you place your cursor at the end and type a command then press <RET>, the command is sent to a subshell and the output is added to the buffer. This allows you to easily search back and forth, copy and paste, select an entire region and even save the buffer content to a file.
The second most important command to learn is M-x ren-buf <RET> NewBufferName <RET>. This command allows you to rename the current buffer. If the buffer contains the content of a file, this will not rename the file. This is useful when you need more than one shell. If you type M-x shell <RET> and you have one open, it will bring forth the existing one. Rename the *shell* buffer to open more shell. Emacs also has eshell and term, but this is beyond our current scope.
This article represents a very short and quick introduction to Emacs. You learned the basic Emacs commands/key bindings and some basic concept about editing with Emacs.
There is a lot more it can do for you. With over 4000 packages available for installation with Emacs integrated package management system. The flexibility it offers is astonishing. Emacs will grow and evolve with you.
For more on Emacs and other Linux subject, follow me on Facebook: Learning Linux with Eric Simard