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?


I did some research and find it is not too hard.

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.

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')
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.

| improve this answer | |
  • From 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 no keys.py access. For a minimum context can tell $ tmux -V vomited tmux 1.6 and uname -a vomited Linux ????????? 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux. – 41754 Nov 6 '13 at 9:52
  • @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. – alick Dec 4 '13 at 9:45
  • I'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 rangers 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 using ranger-master 1.6.1 and ranger 1.5.4 (stable) in two hosts at hand reach, and both fetch rc.conf and none keys.py. – 41754 Dec 5 '13 at 8:03
  • 2
    I 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 + "'") – alick Dec 15 '13 at 13:42
  • 1
    It seems that if the Python is version 3, os.environ doesn't have has_key method. Instead, one could use eval if 'TMUX' in os.environ.keys() – Jason Sep 8 '17 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.