how to open file in another pane in Tmux

5.5k views Asked by At

I'm new to tmux, and split a window into 3 panes, left half(main pane), top-right quarter and bottom-right quarter. Is it possible that I use command to open an editable file in the left main pane, say vim myFile.py, and myFile.py will open in another pane, say top-right pane, such that I can always edit file in the top-right pane and keep input commands int eh left main pane?

3

There are 3 answers

1
dpk2442 On BEST ANSWER

This is possible to do using features of vim, but it has nothing to do with tmux specifically. If the version of vim you are using was compiled with the +clientserver option, then there is a way to do this. To check, run :version in vim, and look for +clientserver. The client server capabilities of vim depend on X11, so if you don't see it, installing gvim might help. See https://unix.stackexchange.com/a/23601 for more information on the X11 dependency.

Now on to how to use it. First, set up your tmux panes however you want. In the pane that you would like to be the dedicated editor, the following command will create a vim "server."

vim --servername tmuxEditor

Now from another pane, you can run

vim --servername tmuxEditor --remote files...

and the editor will open the specified file(s). Other useful commands are

vim --servername tmuxEditor --remote-tab files...
vim --servername tmuxEditor --remote-send keys

The first opens the specified file(s) in separate tabs, and the second sends the specified key sequence to the server.

See http://vimdoc.sourceforge.net/htmldoc/remote.html or run :help remote in vim for more information on vim's client server features.

0
chepner On

TL;DR: you can use tmux to open commands in panes other than the current pane, but dpk2442's solution using vim's client/server features is probably cleaner.


One option is to set remain-on-exit option on the top-right pane, then use the respawn-pane command to start vim in the pane.

Say the pane identifier of the top-right pane is 0:0.1 (session 0, window 0, pane 1; you'll need to adjust this for your setup). You can set the remain-on-exit option using

tmux set-option -t 0:0 remain-on-exit on

(This sets the option for any pane in the window, not just one particular pane.)

Then, use

tmux respawn-command -k -t 0:0.1 vim myFile.py

to open vim in the pane specified by the argument to the -t option. -k` ensures that any command already running in the pane is killed; you can omit it if you know that nothing is currently running in that pane.

Give the length of the command, you'll probably want to define a shell function to launch vim in the desired pane:

start_vim () {
    tmux respawn-command -k -t 0:0.1 vim "$1"
}

so you can just run

start_vim myFile.py 
2
LazyCat01 On

Here's how to open file at existing Vim instance (if it is running inside tmux session):

tmux send-keys -t vim:1.0 Escape ":tabnew path/to/your_file.txt" C-m

where vim:1.0 is pane selector (vim - session name, 1 - index of window, 0 - index of pane).