Skip to content

Instantly share code, notes, and snippets.

@tzutalin tzutalin/autojump.py
Last active Apr 27, 2019

Embed
What would you like to do?
Ranger's settings and plugins
# config/ranger/plugins/autojump.py
import ranger.api
import subprocess
from ranger.api.commands import *
HOOK_INIT_OLD = ranger.api.hook_init
def hook_init(fm):
def update_autojump(signal):
subprocess.Popen(["autojump", "--add", signal.new.path])
fm.signal_bind('cd', update_autojump)
HOOK_INIT_OLD(fm)
ranger.api.hook_init = hook_init
class j(Command):
""":j
Uses autojump to set the current directory.
"""
def execute(self):
directory = subprocess.check_output(["autojump", self.arg(1)])
directory = directory.decode("utf-8", "ignore")
directory = directory.rstrip('\n')
self.fm.execute_console("cd " + directory)
from ranger.api.commands import Command
# https://github.com/ranger/ranger/wiki/Integrating-File-Search-with-fzf
# Now, simply bind this function to a key, by adding this to your ~/.config/ranger/rc.conf: map <C-f> fzf_select
class fzf_select(Command):
"""
:fzf_select
Find a file using fzf.
With a prefix argument select only directories.
See: https://github.com/junegunn/fzf
"""
def execute(self):
import subprocess
if self.quantifier:
# match only directories
command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
else:
# match files and directories
command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
-o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
stdout, stderr = fzf.communicate()
if fzf.returncode == 0:
fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
if os.path.isdir(fzf_file):
self.fm.cd(fzf_file)
else:
self.fm.select_file(fzf_file)
# fzf_locate
class fzf_locate(Command):
"""
:fzf_locate
Find a file using fzf.
With a prefix argument select only directories.
See: https://github.com/junegunn/fzf
"""
def execute(self):
import subprocess
if self.quantifier:
command="locate home media | fzf -e -i"
else:
command="locate home media | fzf -e -i"
fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
stdout, stderr = fzf.communicate()
if fzf.returncode == 0:
fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
if os.path.isdir(fzf_file):
self.fm.cd(fzf_file)
else:
self.fm.select_file(fzf_file)
# config/ranger/rc.conf
map cj console j%space
map DD shell trash %s
set preview_images true
set preview_images_method iterm2
map cf fzf_select
map cl fzf_locate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.