How to Use IRC in Emacs with ERC

You can use Emacs even to chat with your friends online.

Emacs Irc 00 Featured Image

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.

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.

Emacs Irc 03 Irc Rfc Sample

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.

Emacs Irc 04 Erc Info Menu

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.

Getting Started with ERC

  1. In Emacs, press Alt + X to bring up the Command Buffer.
  2. Type erc then press Enter.
Emacs Irc 05 Running Erc
  1. 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.
Emacs Irc 06 Sample Erc Session
  1. 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.
Emacs Irc 07 Irc Server Address Prompt
  1. 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.
Emacs Irc 08 Irc Server Port Prompt

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.

Emacs Irc 09 Undernet Connection
  1. 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.
  2. 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.
Emacs Irc 10 Undernet Join Channel

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.

Emacs Irc 11 Undernet Channel Buffer

Aside from sending messages, you can also run a number of additional commands while in the ERC buffer.

  1. For example, you can press Ctrl + C, then Ctrl + N to get a list of all the users in the current channel.
Emacs Irc 12 Undernet User Command
  1. 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.
Emacs Irc 13 Undernet Copy Buffer
  1. 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.
Emacs Irc 14 Undernet Operator Command

3. Leaving an IRC Channel and Server in Emacs

  1. 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.
Emacs Irc 15 Undernet Leave Channel
  1. 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.
Emacs Irc 16 Undernet Channel Part Message
  1. 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.
Emacs Irc 17 Undernet Server Quit Message

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.

Emacs Irc 18 Erc Modules List

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.

  1. To enable ERC’s Module feature, press Alt + X, then type “customize-option”.
Emacs Irc 19 Customize Option Command
  1. 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”.
Emacs Irc 20 Erc Modules Command
  1. 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.
Emacs Irc 21 Enable Autoaway Module

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.

  1. 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.
Emacs Irc 22 List External Module
  1. 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/
  1. 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")
  1. 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.

  1. Defining new Lisp functions for ERC is relatively straightforward. To do that, you need to first load your init.el file.
Emacs Irc 23 Sample Init El
  1. 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 with erc-mode-map to apply the new Emacs keybinding to ERC.
Emacs Irc 24 Erc Mode Map Help
  1. 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)))

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

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox