DEV Community

Breno Farias Fonseca
Breno Farias Fonseca

Posted on

Increasing productivity by better managing multiple terminals in Emacs

For those who utilize multiple virtual terminals in Emacs, the management of these terminals can become a challenging task. When the terminals are associated with disparate projects, the process of utilizing the terminal and editing code can become significantly slower. To illustrate, consider the scenario of opening three distinct projects, each of which employs a separate terminal. In such a situation, it would be simple to become confused and lose track of the various elements.

I typically utilize vterm in Emacs for the precise reason outlined in its documentation:

The use of compiled code (as opposed to ELisp) enables Emacs-libvterm to function fully, with high speed and the capacity to process large outputs in an uninterrupted and seamless manner.

What methods can be used to improve the management of multiple virtual terminals in Emacs?

The initial action to be taken is as follows: The terminals were designated by the project or context in use.

(defun my-vterm-custom-buffer-name (name)
  "Open a new vterm buffer with a specified NAME."
  (interactive "sEnter buffer name: ")
  (let ((buffer (get-buffer-create (concat "*VTERM-" name "*"))))
    (with-current-buffer buffer
      (unless (derived-mode-p 'vterm-mode)
        (vterm-mode)))
    (switch-to-buffer buffer)))
(global-set-key (kbd "s-t") 'my-vterm-custom-buffer-name)
Enter fullscreen mode Exit fullscreen mode

The function is located in the Emacs configuration file and, when executed, allows the user to specify the name of the desired vterm buffer. In the event that no buffer with the specified name exists, the function will create a new buffer with the name of the argument that was passed, appending β€œ*VTERM-” to the name. This newly created buffer will then be opened. To facilitate the procedure, the command β€œs-t” (command-t in macOS) has been utilized as a shortcut to execute the created function.

The second step is to create a new buffer that will facilitate the opening of all the terminals in split windows.

To accomplish this, one must open as many terminals as necessary in split windows. For those who prefer to split windows on the same vertical axis, the command is C-x 3 (split-window-right). Alternatively, for those who wish to split windows on the same horizontal axis, the command is C-x-2 (split-window-below).

After completing the above steps, it is time to push all open windows into a single buffer. To do this, use the ivy-push-view command, making sure that the ivy package is installed. When running the command, it is necessary to pass an argument specifying the desired buffer name.

Now I can switch to using multiple terminals in a single buffer and locate them quickly.

Dependencies:
vterm: https://github.com/akermu/emacs-libvterm
ivy: https://github.com/abo-abo/swiper

Top comments (0)