trevarj / tmux-open-nvim

A tmux plugin that helps opening files in a Neovim pane
GNU General Public License v3.0
51 stars 8 forks source link
neovim neovim-config tmux tmux-plugin tmux-plugins

tmux-open-nvim

A tmux plugin that helps opening files in a Neovim pane

Demo

old demo

Installation

Using TPM, add this to your .tmux.conf

set -g @plugin 'trevarj/tmux-open-nvim'

Reload tmux config (<prefix>-I). Also you may want to start a fresh session to reload $PATH into your environment.

Create symlink to ton script (optional)

Due to the caveat below, you can create a symlink to the ton script so it can be used no matter what.

# Use any path that is on your $PATH
$ sudo ln -s ~/.tmux/plugins/tmux-open-nvim/scripts/ton /usr/local/bin/ton

Configuration

Available configuration options to put in your .tmux.conf

Config Description Example
set -g @ton-open-strategy ":e" Command for opening a file :e or :tabnew
set -g @ton-menu-style Set style of display-menu for picking a pane See man tmux STYLES
set -g @ton-menu-selected-style Set style of display-menu selection for picking a pane See man tmux STYLES
set -g @ton-prioritize-window true If true and nvim exists in current window, opens directly in that instance. If false, prompts for a selection true or false

Usage

CLI

The plugin will add a helper script called ton to your path while inside a tmux session.

The target use case of this plugin is when you have a tmux window that already has a pane running nvim and a pane with a terminal:

$ ton file.txt # optionally add :[line]:[col] to the end, i.e file.txt:40:5
# Opens file.txt in nvim pane

If you have more than one Neovim instance running in a tmux window, or @ton-prioritize-window is false and you have nvims in other windows, you will be prompted with a tmux display-menu that will allow you to select where to open the file.

Caveat

Upon launch of a fresh tmux session, the script will not be in the first pane due to how an environment is loaded, I guess. I think the only way to resolve this is by adding the ~/.tmux/plugins/tmux-open-nvim/scripts directory to your path permanently or with tmux -e PATH=$PATH:~/.tmux/plugins/tmux-open-nvim/scripts

When you create a session, it creates window 0 automatically, which fires off a shell. So, for that shell, setenv doesn't work and you have to send-keys. But when you create a new window, like with split-window, the new window gets the environment from the setenv. The example shows that both windows have the environment whether set explicitly via export or via setenv.

See:

tmux-fingers (or tmux-open)

An optimal workflow using tmux-fingers:

Add this to your .tmux.conf:

# Overrides matching file paths with :[line]:[col] at the end
set -g @fingers-pattern-0 "(([.\\w\\-~\\$@]+)(\\/?[\\w\\-@]+)+\\/?)\\.([\\w]+)(:\\d*:\\d*)?"
# Launches helper script on Ctrl+[key] in fingers mode
set -g @fingers-ctrl-action "xargs -I {} tmux run-shell 'cd #{pane_current_path}; ~/.tmux/plugins/tmux-open-nvim/scripts/ton {} > ~/.tmux/plugins/tmux-open-nvim/ton.log'"s

Now you can enter fingers mode and use Ctrl+[key] to launch a file in nvim

Future Features

NOTE

You may be interested in a similar workflow that I have created using telescope-tmux.nvim, where you can open up a Telescope picker and it will present to you all the file paths that can be found in every tmux pane. Then, you can select the from the list.

See here for details