DEV Community

Rajasegar Chandran
Rajasegar Chandran

Posted on

Helix and tmux integration

In this post we are going to take a look at how to easily interact with tmux from Helix.

Lately, I have been thinking about switching to Helix completely from Neovim. I have been working on how to achieve proper tmux integration with Helix editor. Previously I have been using a Vim plugin called vimux to run commands quickly from Neovim without leaving the editor.

My workflow usually consists of two main things:

  • Run commands from the editor like npm test
  • Run the same command again after making some code changes

Helix

Helix is a postmodern text editor built in Rust built for the terminal. It has got multiple selections, built-in Tree-sitter integration, powerful code manipulation and Language server support.

One of the best thing about Helix is the no-plugin philosophy. You might not need any plugins, because you already have all the features built in like Fuzzy finder to jump to files and symbols, project wide search, beautiful themes, auto closing bracket pairs, surround integration and more. All you need is to just enable and tweak them in the config file.

Tmux

tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.

Vimux

Vimux is a Vim plugin which helps you to easily interact with tmux from vim. It was originally inspired by tslime.vim, a plugin that lets you send input to tmux. While tslime.vim works well, it wasn't optimized for the use case of having a smaller tmux pane used to run tests or play with a REPL. The goal of Vimux is to make interacting with tmux from vim effortless.

Shell scripts

Let's create a bash script to automate the process. Let's call it hemux, a word play of helix + tmux. I know it sounds crazy right? 😜

If you are using a Linux based distro, go to /usr/local/bin and if you are on MacOS, go to ~/.local/bin

Create a new file and open it in the Helix editor.
Note: If you don't have admin permissions, you need to prefix all the commands with sudo.

touch hemux
hx hemux
Enter fullscreen mode Exit fullscreen mode

Copy paste the following script into the new file and save it.

#!/bin/sh

PANES=$(tmux list-panes | wc -l)
# echo $PANES
if [ "$PANES" -gt 1 ]
  then
    tmux send-keys -t 2 $1 Enter
  else
    tmux split-window -h
    tmux send $1 Enter
fi
echo "$1" > /tmp/prev-tmux-command
Enter fullscreen mode Exit fullscreen mode

Let's see what the bash script does:

  • First, we are getting the number of tmux panes available
  • If there is more than one pane available, we just run the commands in the second pane, otherwise
  • If there is only one (in our case, it is the only pane where we have opened Helix) we will create a new vertical split and run the commands there.
  • Finally, we are storing the last command in a temp file called prev-tmux-command which we will use later, when we are running the command again repeatedly.

Add executable permissions to your bash script like

chmod +x hemux
Enter fullscreen mode Exit fullscreen mode

Now, let's try the command inside a tmux session.

hemux ls
Enter fullscreen mode Exit fullscreen mode

This should open a new split pane if you already don't have one and run the commands inside it. In our case, we are just listing the files inside our directory.

Now inside an Helix editor, you can run them with the run-shell-command command by going into the command mode by pressing : in Normal mode and then typing sh hemux ls

:sh hemux ls
Enter fullscreen mode Exit fullscreen mode

Image description

OK, this is awesome.

Image description

Because now you can send the same command again to the tmux pane open using a new bash script called hemux-last

Let's follow the same steps as above:

touch hemux-last
hx hemux-last
Enter fullscreen mode Exit fullscreen mode

Copy paste the following content in the new file and save it.

#!/bin/sh

PREV_TMUX_COMMAND=`cat /tmp/prev-tmux-command`
tmux send-keys -t 2 $PREV_TMUX_COMMAND Enter
Enter fullscreen mode Exit fullscreen mode

Add executable permission to hemux-last

chmod +x hemux-last
Enter fullscreen mode Exit fullscreen mode

So now running the last tmux command becomes so easy. You can do that by

:sh hemux-last
Enter fullscreen mode Exit fullscreen mode

Image description

I have added a key-binding for the same in my config.toml such as Space-v to run the last tmux command.

[keys.normal.space]
v = ":sh hemux-last"
Enter fullscreen mode Exit fullscreen mode

Image description

Hope you enjoyed the post. Please let me know in the comments section how we can improve this workflow and how can we add additional capabilities to our bash script.

References:

Top comments (1)

Collapse
 
revnixcad profile image
Nico Bos

Thank you for sharing this post. To get it working on my setup, I had to modify the command tmux send-keys -t 2 $1 Enter to tmux send-keys -t 1 $1 Enter, adjusting the pane ID from 2 to 1 in the hemux-last script as well.