Tmux ranger integration: opening text files in new panes
As of 2022, Python 2 is no longer supported. Here is what works for me on ranger 1.9.3 on macOS via Homebrew.
map ef shell [[ -n $TMUX ]] && tmux split-window -h vim %f
or
map ef eval exec('try: from shlex import quote\nexcept ImportError: from pipes import quote\nif "TMUX" in os.environ: fm.run("tmux splitw -h vim " + quote(fm.thisfile.basename))')
It is based on the official ranger wiki with minor tweaks:
- For some reason, I don't have the
rifle
command, so I usevim
instead. - Added checking for $TMUX env, so only open a new tmux pane if ranger is under a tmux session already, as requested in the comment thread.
Note the first way depends on bash (need to tweak [[
part if other shells), and the second way depends on Python shlex or pipes module.
Historical Info Below
To open the current selected file in ranger in a new pane (to the right) in an ad-hoc manner, you can first go to ranger's command line (by pressing :) and then type shell tmux splitw -h vim %f
following by the <Enter>
key.
Note: these methods below do not work with filenames with space characters!
To achieve this with some key binding, you can set it in a configuration file of ranger. For ranger 1.6+, key bindings are specified in rc.conf
. So in ~/.config/ranger/rc.conf
, use something like this:
map ef eval if 'TMUX' in os.environ.keys(): fm.execute_console("shell tmux splitw -h 'vim " + fm.thisfile.basename + "'")
While with ranger 1.4 you need a file ~/.config/ranger/keys.py
with the following contents:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Customized key bindings.
from ranger.api.keys import *
map = keymanager.get_context('browser')
@map("ef")
def edit_file_in_new_tmux_pane(arg):
command = "shell tmux splitw -h 'vim " + arg.fm.env.cf.basename + "'"
if 'TMUX' in os.environ.keys(): arg.fm.execute_console(command)
With the above setting when you press ef
in the ranger's browser, it will open a new tmux pane with vim editing the selected file.
The code is simply for demo, and it might need to involve with more safeguarding, such as checking for file type, etc.
Credit goes to ranger's help manual and $(pythonpkginstalldir)/ranger/defaults/rc.conf
($(pythonpkginstalldir)/ranger/defaults/keys.py
for ranger 1.4). They are really helpful.
Related videos on Youtube
41754
Updated on September 18, 2022Comments
-
41754 over 1 year
Here we have some amazing tools: tmux, ranger, vim... Would be amazing to configure ranger to open the files (when text editable) in a tmux newpane? Is that easy and how it is done?
-
41754 over 10 yearsFrom
tmux
typing:shell tmux splitw -h 'vim %f'<Enter>
worked ok for me, but$ TMPFILE=`mktemp`; strace ranger 2> $TMPFILE; grep keys $TMPFILE; rm $TMPFILE
hinted nokeys.py
access. For a minimum context can tell$ tmux -V
vomitedtmux 1.6
anduname -a
vomitedLinux ????????? 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux
. -
prachi over 10 years@uprego Oh ranger 1.6 does not use keys.py anymore. It uses rc.conf instead for keybindings etc. Sorry for the delay since I just updated my ranger lately and recognized this :( I'll put an update soon.
-
41754 over 10 yearsI'm seeing it working now, and it's good. However I'm not sure this could be a complete use case yet, because I would want that this could be done only from
ranger
s checking that they are inside the scope of a tmux session (what surely can be worked around with different approaches) to not affect an unrelated tmux session (happened). Just a minor point: I'm currently usingranger-master 1.6.1
andranger 1.5.4 (stable)
in two hosts at hand reach, and both fetch rc.conf and none keys.py. -
prachi over 10 yearsI just find tmux will set the environment variable
$TMUX
inside its session, so checking it should be enough. Try this:map ef eval if os.environ.has_key('TMUX'): fm.execute_console("shell tmux splitw -h 'vim " + fm.thisfile.basename + "'")
-
Jason over 6 yearsIt seems that if the Python is version 3,
os.environ
doesn't havehas_key
method. Instead, one could useeval if 'TMUX' in os.environ.keys()
-
prachi over 6 years@Jason seems Python 2 also has
os.environ.keys()
so I've updated the answer with the more compatible way. Thanks! -
Krackout about 2 yearsThe answer still works , Ranger 1.9.3, tmux 3.1c. But not for file names with spaces. Any ideas how to work with them also?
-
prachi about 2 years@Krackout, I updated the answer to use
quote()
to work with file names with spaces. -
Krackout about 2 years@alick, works perfectly, thank you!