Emacs is a wonderful tool that can do just about anything. While it is a text editor, It can function as an email client, RSS reader and even music player too. If you are an Internet Relay Chat (IRC) user, it is also possible to use IRC from inside Emacs. This can be useful if you want to chat with others through IRC but do not want to leave your Emacs buffer.
Also read: 5 Useful Emacs Packages for Better Productivity
What is IRC and ERC?
In a basic sense, IRC is a protocol that allows two or more people to communicate through text over the internet. Unlike newer chat platforms, IRC solely relies on being able to communicate over plain text. This approach, in turn, allows this protocol to be usable even in situations where bandwidth is severely limited.
One of the easiest IRC clients that you can use with Emacs is ERC. It’s an Elisp IRC client that supports features which allow you to modify it to suit your needs. For example, ERC can implement an “Autolog” feature where it saves all chat logs in files for a specific interval.
Further, ERC also comes by default in a standard Emacs installation. This means that you do not need to install external packages or third-party repositories to start.
Also read: How to Create Beautiful Beamer Slides with Emacs
Getting Started with ERC
- In Emacs, press Alt + X to bring up the Command Buffer.
- Type
erc
then press Enter.
- This will bring up a prompt where you can type the address of the server that you want to connect to. By default, Emacs will fill this prompt with the address of the Libera Chat Server. This is a Free Software network where you can find support channels for your favorite open source software.
- In our case, however, we will be connecting to UnderNet. This is an IRC network that allows users to easily create general topics and test channels.
- From here, ERC will then ask for the specific port that you want to connect to. For the most part, all chat networks share the same port number where you can connect to them. Because of that, we can also use the “6667” port when connecting to UnderNet.
Also read: 5 Better USENET Readers for Linux
1. Joining an IRC Channel in Emacs
From there, ERC will use those details to create a new buffer and connect to your chat server. Once done, it will print the server’s welcome message as well as a small prompt where you can type text and commands.
- The next thing that you need to do is to join a specific IRC channel. To do so, you can either type
/join
followed by the channel’s name in the prompt or press Ctrl + C, then Ctrl + J. - Using the latter will open a command buffer where you can type the name of the channel that you want to join. In our case, we’ll be joining the “#hello-world-test” channel.
Also read: How to Manage Git in Emacs with Magit
2. Sending Your First IRC Message
Once inside, you can now start sending messages to the channel by typing after the ERC>
prompt and pressing Enter.
In this example, we’ve typed “Hello world!” in the prompt to send a message to the “#hello-world-test” channel.
Aside from sending messages, you can also run a number of additional commands while in the ERC buffer.
- For example, you can press Ctrl + C, then Ctrl + N to get a list of all the users in the current channel.
- Further, you can also manipulate all the text inside an ERC session. Press Ctrl + Space to visually select any text and then Ctrl + W to copy it to your clipboard.
- ERC also provides Operator-specific commands from within the buffer. This can be especially useful if you are an operator that wants to moderate your channel while inside Emacs. For example, you can press Ctrl + C, then Tab to turn your channel private.
Also read: 5 Hidden Features You Can Use to Improve Emacs
3. Leaving an IRC Channel and Server in Emacs
- To leave the current IRC channel in ERC, you can either type the traditional
/part
command in the prompt or press Ctrl + C, then Ctrl + P while inside the ERC buffer.
- Doing that will bring up a small prompt where you can write your reason for leaving the channel. You can just leave it blank by pressing Enter in the prompt.
- To leave the IRC server, you can either use the traditional
/quit
command or press Ctrl + C, Ctrl + Q. This command will also bring up a small prompt where you can type your reason for quitting.
Configuring ERC
Aside from sending chat texts, you can also modify ERC’s default behavior to suit your needs. For example, ERC allows you to not only add optional features but also create new ones through custom functions.
Also read: How to Manage your Finances with Ledger Mode in Emacs
1. Enabling Optional Features Through Modules
One of the most powerful features of ERC is its ability to load and unload parts of the client through its modules system. This allows you to create a custom ERC instance that only does what you want it to do.
- To enable ERC’s Module feature, press Alt + X, then type “customize-option”.
- This will then bring up a small prompt where you can type an option name to toggle its activation. In this case, you need to provide the value “erc-modules”.
- From there, Emacs will create a buffer with a checkbox list of all the available modules for your machine. For example, I selected and applied the “autoaway” module to enable ERC’s status management.
Also read: How to Use Emacs for RSS with Elfeed
2. Adding a Third-Party ERC Module to Emacs
It is also possible to enable non-standard modules for ERC. This allows you to introduce new custom features without the need to tinker with ERC’s internals.
- First, you need to obtain a copy of the module that you want to install. In our case, we will use a custom “erc-highlight-nicknames.el” module for ERC.
- From there, you need to copy the module file to the load path of your Emacs client. In most cases, this should be your Emacs’ configuration directory. As such, you can run the following command to copy your module to its appropriate directory:
cp -v /path/to/your/module /home/$USER/.emacs.d/
- Once done, you then need to configure your Emacs client to recognize the new third-party module. To do that, you need to add the following line to your init.el file:
(add-to-list 'load-path "~/.emacs.d/") (load "nameofmodule.el")
- Finally, you can restart your Emacs client to apply the new settings.
3. Defining New ERC Functions
Lastly, you can also add new features to ERC by writing Lisp functions directly to your init.el file. Unlike loading a custom module, this allows you to quickly introduce small tweaks to your ERC session.
- Defining new Lisp functions for ERC is relatively straightforward. To do that, you need to first load your init.el file.
- From there, you can use a variety of Lisp functions that directly modify ERC’s behavior. For example, the
define-key
function allows you to create a new keybinding in Emacs. You can then link this function witherc-mode-map
to apply the new Emacs keybinding to ERC.
- Knowing that, the following snippet of code uses these two functions to create a simple nickname query function to ERC:
(define-key erc-mode-map (kbd "C-c C-z") (lambda (nick) (interactive (list (completing-read "Nick: " channel-members))) (erc-cmd-QUERY nick)))
Also read: How to Use Emacs As a USENET Reader With Gnus
Frequently Asked Questions
Is it possible to use ERC with SSL/TLS?
Yes! By default, ERC does not natively support the encrypted IRC ports. This means that ERC will not detect the encrypted connection even if you provide an SSL port when connecting to a server. One way to deal with this is to use a purpose-built fork of ERC that uses SSL. To take advantage of this, all you need to do is to press Alt + X, then type “erc-ssl”. Doing so will load the same prompts and commands similar to a regular ERC session.
Is it possible to dump the log of an IRC channel in Emacs?
Yes! Aside from being able to yank parts of an ERC buffer, it is also possible to pull the entire ERC log and save it to a file. This can be especially useful if you want to keep a record of your conversations in an IRC channel. Knowing that, dumping an IRC chat log in ERC is incredibly easy. Just press Ctrl + C, then Ctrl + L to save all the text in the active log to a file. From there, Emacs will then attempt to save the file to your “/home/$USER/logs” directory.
Is it possible to create multiple Emacs keybindings for ERC?
Yes! Similar to any other Emacs function, you can add multiple ERC keybindings in your init.el file. For example, this is an excerpt of our init.el file where we have two ERC keybindings:
(define-key erc-mode-map "\C-m" 'newline)
(define-key erc-mode-map "\C-c\C-c" 'erc-send-current-line)
One important thing to note, however, is that you need to be wary of the current active keybindings for ERC. To do that, you can press Ctrl + H, then B while in an ERC buffer. Doing that will then tell Emacs to look at all the ERC-specific keybindings currently active for that buffer.
Image credits: Unsplash All screenshots by Ramces Red
Our latest tutorials delivered straight to your inbox