Helix, tmux and lazygit
Tags: productivity, helix, git
Published: 2023-07-11 04:26:22

In this post, we are going to see how to use lazygit with Helix to manage your git repositories.

In the past, with Neovim which I have been using for a very long time, I relied on git plugins like [vim-fugitive](https://github.com/tpope/vim-fugitive) which does not cover all my use cases exhaustively but it just gets the job done. 

Having used [Magit](https://magit.vc/) in [Emacs](https://www.gnu.org/software/emacs/), I couldn't find any solid replacement plugin for my git workflow. Nothing comes close to Magit actually. But recently I heard about [lazygit](https://github.com/jesseduffield/lazygit) which is quite awesome once I stared using it.

## 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.

## Lazygit
Lazygit is a simple terminal UI for git commands, written in Go with the [gocui](https://github.com/jroimartin/gocui) library.



## 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.

## My workflow
I have an IDE setup with tmuxinator where I pre-configure different window and pane layouts. You can check out my dotfiles [here](https://github.com/rajasegar/dotfiles).

This is how I want to use lazygit with Helix and tmux.
- From Helix, I need to open lazygit in a separate tmux pane
- Work with git repository in the new tmux pane using lazygit and return back to Helix

From the requirements above, it looks like we need a key binding to automate all these tasks within Helix. So let's get started.

Helix has got this command `:sh` to run any shell commands from Helix. Using the same, we are going to invoke some tmux commands.
 
First we assume that ,there is already an empty pane open next to our editor pane. We are going to open lazygit in that pane using the command:
```
tmux send-keys -t 2 lazygit Enter
```
Here `2` is the id of the second pane.

Next we are going to move our focus to the pane.
```
tmux select-pane -t 2
```

When we wrap all the above commands into a key binding in our Helix `config.toml` using the keys `Ctrl + g`, it will look something like this:

## Key mapping

```toml
[keys.normal]
C-g = ":sh tmux send-keys -t 2 lazygit Enter && tmux select-pane -t 2"
```


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

## Update
I have updated my key mapping to make use of [tmux popup](https://dev.to/waylonwalker/floating-popups-in-tmux-67) instead of opening or choosing a tmux pane and run lazygit there. This new workflow is super awesome and very accessible.

```toml
[keys.normal]
C-g = ":sh tmux popup -d \"#{pane_current_path}\" -xC -yC -w80% -h80% -E lazygit"
```

Here we are opening a floating window using `tmux popup` command and passing the current directory using the `-d` flag with `"#{pane_current_path}"` as the value. We are also customizing the popup size by making its width and height 80% using the width, height, x and y parameters.

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

A Big Thanks to [kenos](https://matrix.to/#/@hardcoreterminaluser69:envs.net) from the [Helix Matrix community](https://matrix.to/#/#helix-community:matrix.org) for suggesting `tmux popup`



Hope you enjoyed the post. Please let me know for any improvements in the workflow. If you think there is a better tool than lazygit, also let me know how you are using in the comments section below.