Share: Facebook icon - Twitter icon - LinkedIn icon

Emacs package highlight - vterm

Published Tue Jun 06 2023

Tags: emacs


vterm, or emacs-libvterm, is a way of running a terminal inside of Emacs. It may not sound interesting at first, but it has some nice features that set it apart from the alternatives. One of these includes supporting interactive programs. It also feels more a part of Emacs than an external program, unlike alternatives like term. Read on to see more on why I prefer vterm to the alternatives and how you can se it up yourself!

vterm has quickly become my favorite way of running a terminal inside of Emacs. Sometimes it is convenient to have a terminal in a buffer, especially if you run interactive sessions that listens to file changes. It is also convenient to be able to check Github Actions builds using gh run watch directly inside the comfort of your editor!

What makes vterm stand out?

There are also more differences discussed in the readme of the vterm project. One notable difference to other terminal solutions is that vterm is not implemented in elisp, but uses an external C library.

One very obvious con to the project is that the library it is using is in public archive mode in Github. This means that it won't get any more fixes. The Emacs lisp side is still maintained though, even hough it specifies that it should be considered as alpha-stage software. In my experience, it works well enough for me to use it daily. In my experience this is the best way to run terminals inside of Emacs!

Dependencies needed

As mentioned before, vterm uses an external C library to do most of the heavy lifting. It may then not come as a shock that compiling this library is needed. To do this, CMake and libtool is used. Installing these should be super simple on most systems. Homebrew on Mac OS X have both:

brew install libtool cmake

Ubuntu and Debian based systems also have them readily available:

apt-get install libtool-bin cmake

You don't need to worry about building anything yourself beyond this point! The package installation of vterm does it all for you.

Configuration and usage

vterm can be installed directly from source or through Melpa. I use use-package to install and configure it, which looks like this:

(use-package vterm
    :commands vterm
    :custom
    (vterm-always-compile-module t)
    :hook
    (vterm-mode . (lambda ()
		    ;; Settings to mimic dracula I use for zsh.
		    (setq-local buffer-face-mode-face '(:background "#000000" :foreground "#FFFFFF"))
		    (buffer-face-mode 1)
		    (text-scale-adjust 2)))
    :bind
    ("<f7>" . vterm))

The config does the following:

  • Installs vterm.
  • Compiles the dependent C library without prompting me when first installing. If you don't set vterm-always-compile-module you will be prompted if you want to compile the dependency. You may also provide your own build if you want to, though I have not tried that.
  • Adds a hook that sets some style related settings to fit my needs, including an okay zoom level.
  • Binds vterm to the F7 key.

Now you can start a terminal using F7!

vterm running inside of Emacs

You can use Emacs keybindings as you are used to. One example is splitting the window with C-x 2 and then having your source file on one side and a listening process on the other side. Here we see a simple Kotlin file and Quarkus dev mode running, which is super useful to have side by side when developing Quarkus applications (especially when writing your tests and having your little test driven feedback loop!):

Having

(you can then off course navigate between the two using C-x o or any other way you navigate between your buffers with Emacs keybindings)

Note that by default, vterm only handles ONE terminal inside of Emacs.

Closing remarks

You may ask: With this awesome terminal inside Emacs, surely you don't need any other terminal programs? I still use other ones, and don't call me Shirley. (sorry, I had to). The main reason here is that old habits die hard. On my setups I usually have a lot of terminals open at the same time. That should be possible with vterm by using the multi-vterm, though I have not tried that one yet. Who knows, maybe I will replace my many iterm2 tabs with vterm buffers in the future?

There are many configuration options not discussed here, some of them being on the shell side. One of these is directory tracking to have Emacs' default directory following your current directory in vterm. I have not had much need for it, but it is described in the project readme.




Other posts that might interest you: