6

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?

0

1 Answer 1

10
+500

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 use vim 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.

9
  • 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, 2013 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, 2013 at 9:45
  • 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, 2013 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, 2017 at 2:09
  • 1
    @Krackout, I updated the answer to use quote() to work with file names with spaces.
    – alick
    Feb 27, 2022 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .