5 Hidden Features You Can Use to Improve Emacs

Unlock the power of Emacs with these packages you might not have tried.

5 Emacs Hacks 00 Featured Image

Emacs is a wonderful tool and with the right third-party packages you can create your own environment within it. Functions such as window spawning and terminal emulation are just some of the parts of Emacs that are hidden to the casual user. This article aims to shed light on some of those in order to allow you to make the the most out of your Emacs installation.

1. Spawning a New Window

One of the most useful feature in Emacs is the ability to split a window. Using either Ctrl + x, 2 or Ctrl + x, 3, you can easily duplicate your current buffer either horizontally or vertically. This can be especially useful if you are working on different parts of the same file.

5 Emacs Hacks 02 Split Screen

Knowing that, you can also use this Ctrl + x paradigm to split a window for a new file. By default, normal Emacs commands take over the window that you are currently at. For example, if you press Ctrl + x, f and open a file, that file will take over the whole screen.

However, by pressing Ctrl + x, 4 followed by f. You can instruct Emacs to open that file in a separate buffer. This can be incredibly useful if you want to check a new file but you also want to retain the buffer the you are currently working on.

5 Emacs Hacks 18 Switch To New Window

Further, you can also use this to switch to a currently open the buffer in your Emacs session. To do that, you can press Ctrl + x, 4 then b to instruct Emacs to open a command buffer. This, in turn, lists all of the currently open buffers in your session.

5 Emacs Hacks 03 Switch To Buffer Window

2. Terminal Emulators

By default, Emacs comes with two built in shells. The first is a native command interpreter written in Emacs Lisp called “eshell”. It is a text-only shell that you can use to run UNIX commands straight from Emacs.

Being text-only, you can also modify any command output in eshell similar to any Emacs text buffer. This can be useful if you are in a debugging session and you want to make notes in between the compilation errors.

5 Emacs Hacks 05 Eshell

Due to the nature of eshell, it cannot run any complex Terminal UI (TUI) program. For that Emacs also ships with term, a VT100 terminal emulator.

Similar to a regular Linux terminal, term emulates all the functions of a VT100 including its escape sequences. This means that you can run any TUI application in Emacs. For example, I am currently running vim within Emacs through term.

5 Emacs Hacks 06 Vim Inside Emacs

With that, you can run both eshell and term by pressing Alt + x. This will bring up the Emacs command buffer. From there, you can either type eshell to load the text terminal or you can type term to load the VT100 Emulator.

One important thing to note is that term also has a limited “text-only” mode that you can access by pressing Control + c, j.

5 Emacs Hacks 07 Terminal Line Mode 1

This will turn the current term session to “line mode” where you can easily copy and paste commands to and from the command prompt. From there, you can go back to a regular terminal mode by pressing Ctrl + c, k.

3. Accessing Remote Files in Emacs

Another brilliant built-in program in Emacs is the Transparent Remote File Access, Multiple Protocol (TRAMP). This is a simple program that allows you to seamlessly access any file through any protocol within Emacs.

5 Emacs Hacks 08 Sample Tramp

It works by using helper programs such as ssh, rlogin and su to connect the user to a remote computer. From there, TRAMP mounts the remote computer’s files to the user’s local machine. That, in turn, makes those files appear as if they are local.

Knowing that, you can use TRAMP by pressing Ctrl + x, f and Ctrl + Backspace to clear the file prompt. From there, you need to type a specific path format to tell Emacs to use TRAMP:

For example, I typed the following TRAMP path to connect to my file server through SSH:

You can also use TRAMP to edit privileged files inside Emacs. This is highly useful if you need to quickly edit system files and you do not want to pull up a privileged editor. Doing it this way also reduces the risk of security issues since TRAMP creates a sandbox for the privileged file.

To access a privileged file through TRAMP, you need to use su as your protocol:

In this example, I was editing my Yggdrasil configuration file through TRAMP. Since I only accessed the file locally I didn’t need to provide an address to su.

Once I pressed Enter, TRAMP asked for my user password. After that, the file loaded in an Emacs buffer as if it is just an unprivileged file.

5 Emacs Hacks 09 Privilaged File In Emacs

The default Emacs search function is relatively clunky. You can press Ctrl + S to search forward and Ctrl + R to search in reverse. However, this form of searching is strictly limited to matching a specific string.

This means that anything you type in the search box will be exactly matched, including spaces. For most cases, this is not an issue since most text searches only consist of single words and short phrases.

5 Emacs Hacks 10 Search Sample

Despite that, there might be instances where you want Emacs to do a fuzzy search. This is a type of text search that only matches small parts of multiple strings.

For example, using the default Emacs search it is not possible to search for the phrase “hello world” by just providing “he ld” in the search box.

5 Emacs Hacks 11 Sample Regular Search

To make this possible, you will need to add the following Lisp code to your init.el file:

  • The first function, search-whitespace-regexp replaces the whitespace character for any character (.*) when using text search.
  • The second function, isearch-lax-whitespace enables multiple whitespace matches when searching for a single whitespace. Using this in conjunction with the first function allows you to do a fuzzy search.
  • Lastly, the third function, isearch-regexp-lax-whitespace makes sure that you would only need to use a single whitespace character to match create a fuzzy search.

Knowing that, you can now reload your Emacs client and test whether fuzzy search works. In my case, I was able to search for the phrase “hello world” by just typing “he ld”.

5 Emacs Hacks 12 Sample Fuzzy Search

5. Editing Your Dired Buffer

Dired is the default file manager for Emacs. At its core, it allows you to easily go through your computer’s filesystem and open any file straight to an Emacs buffer. You can easily access Dired by either pressing Ctrl + x, d or Ctrl + x, f.

5 Emacs Hacks 13 Dired Example

However, one of the main issues with Dired is that it is tricky to modify any file in its buffer. For example, if you want to rename a file within Dired you need to do two things:

  • First, you need to mark the file that you want to edit by selecting it and pressing M.
  • From there, you need to then press Shift + R to toggle a rename and provide a new name for your file.
5 Emacs Hacks 14 Normal Dired Rename

While renaming a file this way is enough for basic usage, doing this to modify multiple files can be a challenge.

In order to deal with that, Emacs also comes with the ability to edit the Dired buffer directly as text. This will allow you to not only rename multiple files but also modify their permission bits.

To do this, you can press Ctrl + x, Ctrl + q. This will then enable the “Editable Dired” mode. From here, you can now edit the Dired buffer and, once you save it, those changes will be applied to your files.

For example, if you want to rename multiple files you can just type over the current file names and write new ones. Once done, just press Ctrl + C, Ctrl + C to save changes and those files will be renamed.

5 Emacs Hacks 15 Editable Dired

Changing File Permissions Using Dired

As discussed above, another thing that you can do in Editable Dired is to update the permission settings of files. In order to do that, you will need to add the following line to your init.el file:

5 Emacs Hacks 16 Init El Config

With that done, you can now edit the permission bits of files directly from Editable Dired. For example, consider the following file:

The first column of text indicates that this file has both read and write permissions for me (rw-). Further, it also only has both read permissions for anyone else (r–r–).

5 Emacs Hacks 17 Dired Permission Bits

In order to change this, I selected over this column of text and pressed W to the other bits to allow other people to edit this file:

On the other hand, to undo that you can press - to any permission bit and it will clear the permission for that field:

That’s it, you now know 5 hidden features in default Emacs that you can use to improve your editing experience!

Frequently Asked Questions

1. Is it possible to create a new window for other Emacs packages?

Sadly no. The Ctrl + x, 4 command only works for opening new files similar to Ctrl + x, f and opening buffers similar to Ctrl + x, b.

One way to work around this limitation is to open the package normally using Alt + x. From there, you can bring back your previous buffer by pressing Ctrl + x, 4, b.

2. Can I use a different shell for the VT100 Emulator?

Yes! When you run the term command using Alt + x, Emacs will prompt you for the shell that you want to use. By default, it will add the default shell for your system. This means that if your default shell is bash, Emacs will provide “/bin/bash” in this prompt.

In order to use a different shell, you need to replace this value with the path of the shell that you want to use. For example, if you want to use ksh in term you need to write /bin/ksh when term prompts you for the terminal that you want to use.

3. Can I undo my changes to the Dired buffer?

Yes and no. You can undo all the changes that you have made in Editable Dired by pressing Ctrl + c, ESC. However, this will only work if you have not yet saved the changes to disk.

Once you press Ctrl + C, Ctrl + C, Emacs will write all the changes that you have made. By then, the only way to undo those is to manually edit the files again.

Image credit: Unsplash

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox