From 64e5b99f8b2e50970aa75d2698284f514f4b983a Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Mon, 17 Oct 2016 12:30:47 +0530 Subject: [PATCH] ranger: Add fzf_select command and C-f binding --- .config/ranger/commands.py | 29 +++++++++++++++++++++++++++++ .config/ranger/rc.conf | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.config/ranger/commands.py b/.config/ranger/commands.py index 6ad61b74..572124b9 100644 --- a/.config/ranger/commands.py +++ b/.config/ranger/commands.py @@ -165,3 +165,32 @@ class toggle_flat(Command): self.fm.thisdir.unload() self.fm.thisdir.flat = 0 self.fm.thisdir.load_content() + +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) diff --git a/.config/ranger/rc.conf b/.config/ranger/rc.conf index aa10bc21..37609372 100644 --- a/.config/ranger/rc.conf +++ b/.config/ranger/rc.conf @@ -176,4 +176,5 @@ map zi set preview_images=True map zI set preview_images=False ## toggle_flat -map zF toggle_flat \ No newline at end of file +map zF toggle_flat +map fzf_select