Ranger自定义命令将文件移动到预先指定的目录?

时间:2017-10-07 20:26:28

标签: bash shell

使用游侠,

  1. 如何创建:command将当前选定的文件移动到预先指定的目录?比如说,选择一个file并输入:move_to_path就会运行

    mv文件/路径/位置/文件

  2. 我怎么能像(1)那样做,而是绑定一个键:command?通过突出显示文件并键入mf来说明它在所选文件上运行:move_to_path

  3. 修改

    关于这个游侠的问题是:https://github.com/ranger/ranger/wiki

1 个答案:

答案 0 :(得分:2)

我不是游侠用户,但我在游侠维基中看到了一个似乎有用的条目:https://github.com/ranger/ranger/wiki/Commands

TL; DR:编辑文件〜/ .config / ranger / commands.py

from ranger.api.commands import Command
class move_to_path(Command):
    """
    :move_to_path
    Move file to a directory
    """
    def execute(self):
        import shutil # for shutil.copy, os.rename works fine too
        shutil.move(self.fm.thisfile.path, "/your/directory/" + self.fm.thisfile.basename)

现在您可以使用以下命令启动命令:move_to_path。您可以编写python代码以了解在何处获取目录名称:fixed,在您选择的配置文件中等。

现在,要添加键绑定,请查看:https://github.com/ranger/ranger/wiki/Keybindings或:http://ranger.nongnu.org/ranger.1.html#KEY-BINDINGS 如果我没错,你可以编辑〜/ .config / ranger / config / rc.conf并在这里添加一个键绑定。 例如,您可以添加一行:

map mf move_to_path

我认为这应该可以解决问题。 谢谢你让我发现游侠,明天我会试一试:)

编辑: 要移动多个选定的文件,您可以执行以下操作:

    def execute(self):
        import shutil # for shutil.copy, os.rename works fine too
        from os import basename
        for file in self.fm.thistab.get_selection():
            shutil.move(file, "/your/directory/" + basename(file))