如何在 Ranger 文件管理器中定义新命令?

Cir*_*郝海东 5 ranger

我希望能够通过键入以下内容在Ranger 文件管理器中定义新命令:

:newcmd myarg
Run Code Online (Sandbox Code Playgroud)

并用它运行任意代码。

命令定义还应该能够访问程序的状态,例如当前目录和选定的文件。

有没有办法做到这一点?

免责声明:由于缺乏关于这个主题的良好资料,我创建了这个问题并自我回答。非常欢迎其他答案。

Cir*_*郝海东 4

编辑~/.config/ranger/commands.py以包含类似以下内容:

from ranger.api.commands import *

class newcmd(Command):
    def execute(self):
        if not self.arg(1):
            self.fm.notify('Wrong number of arguments', bad=True)
            return
        # First argument. 0 is the command name.
        self.fm.notify(self.arg(1))
        # Current directory to status line.
        self.fm.notify(self.fm.thisdir)
        # Run a shell command.
        self.fm.run(['touch', 'newfile')
Run Code Online (Sandbox Code Playgroud)

现在您可以输入:

:newcmd myarg
Run Code Online (Sandbox Code Playgroud)

运行定义的命令。

更多选项可以在以下位置找到:https://github.com/hut/ranger/blob/9c585e48e14525f11d2405ea0bb9b5eba92e63e9/ranger/config/commands.py

然后,您可以更进一步并为其定义一个映射,例如:添加到~/.config/ranger/rc.conf

map ,n console newcmd
map ,m newcmd default-arg 
Run Code Online (Sandbox Code Playgroud)

现在您只需输入:

  • ,n在状态行上写入newcmd,并准备好让用户输入第一个参数
  • ,m并使用默认参数立即运行命令

在游侠 1.6.1 上测试。