It's possible to set up vim to edit multiple files without having to exit at all. You use multiple buffers and switch between them. Since you're using vim inside screen You can put the following in your ~/.vimrc to enable that.
set hidden
syntax enable
source $VIMRUNTIME/menu.vim
set wildmenu
set cpo-=<
set wcm=<C-Z>
map <F4> :emenu <C-Z>
nmap <Esc>[5D :bp<CR>
nmap <Esc>Od :bp<CR>
nmap <Esc>OD :bp<CR>
nmap <Esc>[5C :bn<CR>
nmap <Esc>Oc :bn<CR>
nmap <Esc>OC :bn<CR>
nmap <Esc>[3~ :bd<CR>
nmap ZZ :bd<CR>
Those key mappings map to Ctrl-Left and Right arrow keys to switch between buffers. The F4 key invoke the same menu the GUI would show. Pressing tab also now displays a little menu for selecting the completion when your press tab.
tmux and screen have different models so there is no exact equivalent.
In screen terms, a split lets you display multiple windows at the same time. next
(C-a n
) rotates windows through the active part of the split; this lets you rotate “hidden” windows through the active region of the split.
In tmux terms, a split divides a window into one or more panes. Each part of a split window is an individual pane, panes are never hidden (if a window is selected (visible) all its panes are, too), and a pane can only be used in a single split of one window (a pane can not be in multiple windows, and it can not be in multiple splits of the same window). There are commands to move panes around in (or between) windows, but not in an identical way to next
in screen.
You could use a binding like the following to arrange a similar effect:
bind-key C-n swap-pane -s :+.top \; rotate-window -Ut :+
You will probably want to put this in your ~/.tmux.conf
file, but you can just type/paste it after Prefix : to bind it in your current server instance.
To use the binding, pick your “main window”, split it, create a “pane container” window immediately after the “main window”, then use the binding to rotate any pane in the “main window” among the group in the “pane container” window.
Here is how you might create the setup:
Pick a window to use as your “main window”. Start (e.g.) Vim in it.
Split your “main window” into two panes.
E.g. Prefix " (:split-window
)
You can use this pane as your testing window (or log viewer, or whatever).
Create a new window (the “pane container”) immediately after your main window.
E.g. Prefix c (:new-window
)
It is important that no other window gets between the indexes of the “main window” and the “pane container” window (+
in the window specifiers used in the bound commands means “the next higher numbered window”).
Split this window into a number of panes. To rotate through three panes, split this window into two panes (the third pane is the one in the “main window”).
Maybe you need a shell for git, and a shell for running a database interface. Put each in a separate pane in this “pane container” window.
Switch back to your “main window”. Select the pane that you want to “rotate out”.
You can use Prefix Up/Down/Left/Right to move among the panes.
Invoke the binding to swap the current pane with the first pane in “pane container” window and (“behind the scenes”) rotate the panes inside the “pane container” window (so that the next time you run the binding, the first command swaps with the “next” pane in the sequence).
Prefix Control-n (the binding use C-n, but you could change this to whatever you like).
To scroll backwards through the panes, you can use the below:
bind-key C-p swap-pane -s :+.bottom \; rotate-window -Dt :+
Best Answer
It doesn't tell me everything, but I used
fuser ~/.myfile.txt.swp
which gave me the PID of the vim session. Runningps aux | grep <PID>
I was able to find out which vim session I was using, which gave me a hint as to which window I had it open in.Thanks to Giles's inspiration and a bit of persistence and luck, I came up with the following command:
To explain what this does:
This creates a subshell and sets
FNAME
as an environment variable. It's not, strictly speaking, necessary - you could just replace$FNAME
with the filename yourself, but it does make editing things easier. Now, working from the inside out:This produces only the PID of the process that has open the file.
This produces the
pts
of the PID that we found usinglsof
.This produces a pane list of entries like
session:0.1 /dev/pts/1
. The first part is the format that tmux likes for targets, and the second part is thepts
This filters our pane list - the trailing
$
is so it will only match the one we care about. I discovered that quite by accident as I hadpts/2
andpts/22
, so there were two matches, whoops!This produces the
session:0.1
part of the pane output, which is suitable for passing totmux switch -t
.This should work across sessions as well as panes, bringing to focus the pane that contains your swap file.