Ranger: Evaluating Shell Commands Inside Ranger Commands

Created on 19 Jan 2015  路  4Comments  路  Source: ranger/ranger

It would be nice if there was some command or syntax to use the output of a shell command in a ranger command. I think this would be more versatile and less tedious than creating different specific ranger commands. The most obvious example for me would be cd. I think it would be nice to, for example, use things like fasd in ranger. There's also a script I use to cd to the most recently updated subdirectory of different directories (determined by file modification time).

I imagine it looking something like this:

# syntax
:cd $(/path/to/recent_dir.sh)
# or specific command for combining with ranger command on left and shell command on right
# first semicolon denotes break.. or use some other syntax
:shellinsert cd ; fasd -d pattern | awk 'END{print $2}'
enhancement

Most helpful comment

Ranger has a function in its library that could be used in an :eval command: ranger.ext.spawn.
:eval from ranger.ext.spawn import spawn; fm.cd(spawn("echo /").strip())
The strip() removes the trailing newline.

However this is obviously not ideal and I want to implement your suggestion, though I don't know yet what's the best syntax for it. Your first suggestion seems good, but it would require some sort of escaping in the command line, which isn't implemented yet

All 4 comments

I believe you could achieve most of it with eval as a temporary workaround (though I didn't try). As for fasd, take a look at this (and I strongly recommend my fork of fasd due to a security bug (the original version was not maintained for a few years)).

I've never seen eval used with anything and just tried messing with it to no avail. How exactly would you go about doing this? To use subprocess, you have to import it first, and I couldn't get that working. I tried something like :eval fm.cd(os.system("command")). I'm not sure if that would work. It didn't give me an error message but it screwed up how ranger looked and didn't work.

I know it's not too hard to make a command for fasd or whatever shell program, but I think having a way to do this without creating commands would much more convenient (less repeated work and could be used for a single instance of something you wanted to do).

Thanks for the info. It looks like the original author has not been active at all.. sad that there's no mention of this or a transfer of maintainership.

Ranger has a function in its library that could be used in an :eval command: ranger.ext.spawn.
:eval from ranger.ext.spawn import spawn; fm.cd(spawn("echo /").strip())
The strip() removes the trailing newline.

However this is obviously not ideal and I want to implement your suggestion, though I don't know yet what's the best syntax for it. Your first suggestion seems good, but it would require some sort of escaping in the command line, which isn't implemented yet

There is very little value in this over a custom command.

Was this page helpful?
0 / 5 - 0 ratings