Ranger 工具的安装和使用

Github 相关

介绍

在 github 上和 archwiki 上有很多有用的资料.

ranger 是一个命令行下的文件管理器 (file manager).

可以在 man ranger/usr/share/doc/ranger/HACKING.md 中查看信息.

对于配置,可以查看 /usr/share/doc/ranger/config 目录下的文件来仿照着配置, 或者用 ranger --copy-config=all 直接将默认配置复制到用于的配置目录, 一般是 ~/.config/ranger 目录.

/usr/share/doc/ranger/examples/ 中包含了脚本和插件的示例.

安装

先克隆下仓库:

1
git clone https://github.com/ranger/ranger.git --depth=1

运行:

1
sudo make install

基本使用

使用 h j k l 来 navigate.

Enter 来打开文件。

q 来退出.

部分快捷键

  • 重命名文件或目录: cw
  • 修改文件名: a 和 A
  • 剪切一个文件: dd
  • 复制在另一个目录下: pp
  • 删掉一个文件: dD
  • 选中多个, 使用空格.
  • 选中全部, 按 v.
  • bulkrename 命令, 在 vim 下修改文件名.
  • 显示隐藏文件: zh
  • 显示文件大小: dU
  • 排序: o 加一个键
  • 移动上层文件夹: [ 或 ]
  • 在历史记录中跳转: H L
  • 进入 ranger 所在的文件夹: S
  • 复制文件路径: yp
  • 进入任务管理器: w, 取消任务: dd

各个配置文件作用的简要说明

在运行 ranger --copy-config=all 之后, ~/.config/ranger 目录下的文件结构为:

1
2
3
4
5
6
.
├── commands_full.py
├── commands.py
├── rc.conf
├── rifle.conf
└── scope.sh

commands_full.py

commands_full.py 文件中记录了 ranger 中所有的默认指令的定义以及其文档. 但其不会被 ranger 读取, 只是作为一个 reference.

commands.py

正如其名 “commands”, 通过编写 python class 来定义 ranger 的命令. 用户自己编写的代码一般放这里.

wiki 下面的 custom command, 可以把这些添加到这个 commands.py 文件内.

rc.conf

rc.conf 即 “runtime configuration”, 是 ranger 主要的配置文件.

map 命令用于映射:

1
map M cnsole mkcd%space

这里的 mkcd 就是 commands.py 下定义的函数.

console 就是执行一个 ranger 的终端命令.

rifle.conf

rifle.confrifle 命令的配置文件. 文件中定义了文件类型和相应的打开方式, 允许用户在 Ranger 中以适当的方式打开不同类型的文件.

rifle, ranger’s file opener, 即 ranger 中用于打开文件的命令.

scope.sh

scope.sh 文件用于配置 ranger 中的文件预览相关.

在这个文件中, 用 case 语句根据文件的 MIME 类型或文件扩展名, 决定如何预览不同类型的文件. 并通过指定不同的外部程序, 实现对文本文件, 图片, 视频等各种文件类型的预览功能. 例如, 可以使用 highlight 命令来预览文本文件, 使用 w3m 命令来预览网页文件, 使用 ueberzugicdiff 等工具来预览图片和视频文件.

插件安装

Ranger 的插件一般放于 ~/.config/ranger/plugins 目录下.

当 Ranger 启动时, 其会检查 ~/.config/ranger/plugins 目录是否存在, 如果存在则会遍历该目录下的所有文件和子目录, 并加载 .py 文件.

如安装 https://github.com/maximtrp/ranger-archives, 一个用于解压缩的插件:

1
2
cd ~/.config/ranger/plugins
git clone https://github.com/maximtrp/ranger-archives

即可.

为 ranger 编写 commands

这里主要参考 commands_full.py 文件的内容, 这个文件一般位于 /etc/ranger~/.config/ranger 目录下.

一般自定义的命令都编写在 /etc/ranger/commands.py~/.config/ranger/commands.py 文件中. 在这里定义的所有 class (需要时 Command 的 subclass), 都会被作为 ranger 的 command. 这个 class 需要定义如下接口:

  • execute(), 当 command 被调用时执行
  • cancel(), 当 console 关闭时执行
  • tab(tabnum), 当按下 <TAB> 时执行, tabnum 为 1 表示 <TAB>, 为 -1 表示 <S-TAB>
  • quick(), 当按下任意按键时执行

tab() 函数的返回值可以为:

  • None, 即用 <TAB> completion 不起作用
  • String, 将当前 console 的命令换为这个 string
  • List/Tuple/Generator, 循环替换为里面的元素

quick() 函数的返回值为:

  • False, 表示没有命令执行
  • True, 表示之后会运行一个命令

execute()cancel() 函数的返回值不重要.

会被自动传入和定义的成员和方法:

  • self.line, attributes, string, 保存当前在 console 中输入的内容
  • self.args, attributes, list, 包含 command 的所有参数
  • self.quantifier, attributes, string, 比如有一个 command 映射为按下 K 触发, 当按下 6K 时, 这个变量的值则为 6
  • self.arg(n), method, 返回 string, 获取第 n 个参数, 当指定参数不存在时返回空字符串
  • self.rest(n), method, 返回 string, 获取第 n 个及其以后的参数, 如 search foo bar a b c, 则 rest(2) 返回 bar a b c
  • self.start(n), method, 返回 string, 获取第 n 个参数及其之前的所有内容, 如 search foo bar a b c, 则 start(2) 返回 search foo

一些通用的 ranger functions 和 objects (“fm” 指 “file manager”):

  • self.fm, 一个 fm object 的引用, 包含 ranger 的绝大多数信息
  • self.fm.notify(string), 在 screen 打印出指定 string
  • self.fm.notify(string, bad=True), 用红色字体输出指定 string
  • self.fm.reload_cwd(), 重新加载 current working directory (可能是添加了一个文件后刷新下显示的列表)
  • self.fm.thisdir(), 当前目录 (一个 File object)
  • self.fm.thisfile(), 当前文件 (也是一个 File object)
  • self.fm.thistab.get_selection(), 一个包含 selected file 的 list
  • self.fm.execute_console(string), 将 string 作为 ranger command 执行
  • self.fm.open_console(string), 打开一个 console 并填写好指定 string (相当于把命令字符串输好了, 就差回车执行)
  • self.fm.move(direction), 移动光标, 比如 down=3, up=5, right=1, left=1

一个 File object 包含的 attributes 和 methods (“tfile” 指 “temporary file”):

  • tfile.path, attributes, 文件的路径
  • tfile.basename, attributes, 文件的 base name
  • tfile.load_content(), method, 强制加载一个 directory 的内容 (只对 directory 有用)
  • tfile.is_directory(), method, 判断 File object 是否为目录

常用 method

运行 shell 命令

self.fm.execute_command(commands, flags=flags_content), commands 是一个 string, 包含要执行的命令,

打印文本

1
self.fm.notify(self.rest(1))

示例

先解释下定义 command 的文件中导入的 class 和 function:

1
2
3
4
5
6
7
8
9
from __future__ import (absolute_import, division, print_function)

from collections import deque
import os
import re
from io import open

from ranger import PY3
from ranger.api.commands import Command
  • from __future__ import (absolute_import, division, print_function), 是一种特殊的导入语句, 用于在当前脚本中启用一些未来版本的 Python 特性
  • from ranger import PY3, PY3 是一个布尔值变量, 表示当前的 python 环境是否是 python3
  • from ranger.api.commands import Command, Command 是 ranger 命令的基类, 通过继承该类类定义命令

~/.config/ranger/commands_full.py 文件中定义的最简单的命令 echo 为例:

1
2
3
4
5
6
7
8
class echo(Command):
""":echo <text>

Display the text in the statusbar.
"""

def execute(self):
self.fm.notify(self.rest(1))
  • class echo(Command):, 这里 echo 是要定义的命令名, (Command) 是继承自 Command

一般需要在开头用多行注释来声明该命令的用法以及作用.

可以编写一个 “hello world” 程序如:

1
2
3
4
5
6
7
class hello(Command):
""":hello

Print hello world, celebrate the first plugin
"""
def execute(self):
self.fm.notify("Hello World, ranger plugin!")

添加到 ~/.config/ranger/commands.py 文件后:

在 user interface console 输入 :hello 并回车执行:

rc.conf 的配置语法

该文件中的所有命令都会在 user interface 初始化之前运行. 因此在该文件中, 无法运行一些在 UI 中的指令, 如 :delete, :mark.

设置 options

设置 option 的语法为:

1
set option_name option_value

设置 local options

设置只对一个目录起作用的 local option, 语法为:

1
setlocal path=/your/local/dir option_name option_value

给 command 起别名

语法为:

1
alias alias_name command_name command_args

键盘映射

定义 user interface 中的 keymapping

语法为:

1
map key command_name command_args

将一个键值对应的作用 copy 给其他键

语法为:

1
copymap src_key dest_key1 dest_key2...

如:

1
copymap q ZZ ZQ

此时, 按下 ZZZQ 都等价于按下 q.

定义 console 中的 keymapping

console 指, 在 user interface 中, 按下 : (默认的键) 呼出的输入界面.

语法为:

1
cmap key command_name command_args

如:

1
cmap <tab> eval fm.ui.console.tab()

定义 pager 中的 keymapping

pager 指, 在选中一个文件, 按下 i (默认的键) 时进入的预览界面.

语法为:

1
pmap key command_name command_args

如:

1
pmap <down> pager_move down=1

定义 taskview 中的 keymapping

taskview 指, 在 ranger user interface 中, 按下 w (默认的键) 时进入的界面.

语法为:

1
tmap key command_name command_args

如:

1
tmap <up> taskview_move up=1

Ranger 工具的安装和使用
http://example.com/2022/09/05/Ranger-工具的安装和使用/
作者
Jie
发布于
2022年9月5日
许可协议