Ranger Custom Command to Move File to Pre-Specified Directory?

Viewed 1174

Using ranger,

  1. How can create a :command which moves the currently selected files to a prespecified directory? Say, by selecting a file and typing :move_to_path it runs

    mv file /path/location/file

  2. How could I do the same as with (1) but instead bind a key :command? Say by highlighting a file and typing mf it runs :move_to_path on the selected file.

Edit

The question is about this ranger: https://github.com/ranger/ranger/wiki

1 Answers

I'm not a ranger user, but I saw an entry in the ranger wiki that seems to be helpful : https://github.com/ranger/ranger/wiki/Commands.

TL;DR : edit the file ~/.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)

You now have your command that you can launch with :move_to_path. You could write python code to know where to get the directory name : fixed, in a configuration file of your choice, etc.

Now, to add a keybinding, let's have a look at : https://github.com/ranger/ranger/wiki/Keybindings or at : http://ranger.nongnu.org/ranger.1.html#KEY-BINDINGS If I'm not wrong, you can edit the ~/.config/ranger/config/rc.conf and add a keybinding here. For instance, you can add a line :

map mf move_to_path

I think this should do the trick. And thank you for making me discover ranger, I'll try it tomorrow :)

Edit : To move multiple selected files, you could do :

    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))
Related