Helix and tmux integration
Tags: productivity, helix, tmux
Published: 2023-07-10 04:01:34

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](https://helix-editor.com) 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](https://github.com/preservim/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](https://helix-editor.com) is a postmodern text editor built in [Rust](https://www.rust-lang.org/) 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](https://github.com/tmux/tmux/wiki) 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
```

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

```sh
#!/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
```

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
```

Now, let's try  the command inside a tmux session. 
```
hemux ls
```
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
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx0ux49ef9ehub2izjer.png)

OK, this is awesome. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh5phv08duoriw7rlolj.png)

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
```

Copy paste the following content in the new file and save it.
```sh
#!/bin/sh

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

Add executable permission to `hemux-last`
```
chmod +x hemux-last
```

So now running the last tmux command becomes so easy. You can do that by
```
:sh hemux-last
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6pbf32xc2aoibxtcl4e.png)

I have added a key-binding for the same in my `config.toml` such as `Space-v` to run the last tmux command.
```toml
[keys.normal.space]
v = ":sh hemux-last"
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8blau7aijmunbx1yzuff.png)

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:
- [Helix editor](https://helix-editor.com)
- [tmux](https://github.com/tmux/tmux/wiki)
- [vimux](https://github.com/preservim/vimux)
- [Get number of tmux panes](https://unix.stackexchange.com/questions/186390/tmux-get-number-of-panes-in-the-current-window-in-bash-variable)
- [Super Guide to the split-window tmux Subcommand (and Beyond)](https://gist.github.com/sdondley/b01cc5bb1169c8c83401e438a652b84e)