A Rex plugin that runs a command of your choice on a file path you've copied, in a floating pane.
- Double-click a filename in a terminal, such as
sample.bam. This needs Copy on Selection on (Settings > Terminal > Selection), or press Cmd+C after selecting. Drag-select names that contain spaces. - Press Option+Cmd+O, or run Run Command on Copied Path… from the command palette.
- Pick a command from the list.
The command runs in a floating pane on the terminal's machine, starting in the file's folder. The pane closes when the command exits, and closing the pane stops the command. A command that fails within a quarter second leaves its pane open so you can read the error.
The copied text is resolved the way the terminal would resolve it, so a
relative name works from the terminal's current folder. Quotes, a file://
prefix, and a grep-style :LINE:COL suffix are stripped. Folders work too.
require("filecmd").setup{
commands = {
bamheader = "samtools view -H {path} | less",
flagstat = { run = "samtools flagstat {path}", hold = true },
idxstats = { run = "samtools idxstats {path} | less", title = "Reads per contig", interactive = true },
hex = false, -- drop one of the built-in examples
},
}Each entry is either a command string or a table with these fields:
| Field | Meaning |
|---|---|
run |
Required. The shell command. |
title |
A description shown in the picker. |
hold |
Keep the pane open after the command exits, showing [exit N] Press Enter to close. Use it for commands that print and exit, like wc or samtools flagstat. Commands that page through less don't need it. |
interactive |
Read your interactive shell startup files, such as ~/.zshrc, first. Turn it on when the program is only on your PATH after an interactive shell sets it up, as with conda's conda init block. |
Placeholders:
| Placeholder | Becomes |
|---|---|
{path} |
The file's absolute path. |
{name} |
The file's name. |
{dir} |
The folder that holds the file. |
Commands are listed in the picker alphabetically by name. Your commands are added to the built-in examples:
| Name | Command |
|---|---|
view |
less {path} |
head |
head -n 100 {path} | less |
tail |
tail -n 100 -f {path} | cat -v |
info |
ls -ldh, file, and wc -l, through cat -v (holds) |
hex |
od -A x -t x1z {path} | less |
du |
du -sh {path} | cat -v (holds) |
None of the examples sends raw file contents, or a raw file name, to the
terminal. Output goes through less or cat -v, which show control
characters as text. See Security notes.
The file name is never pasted into the command text. Each placeholder becomes
a quoted shell parameter ("$1", "$2", "$3"), and the values are passed
as separate arguments:
sh -c 'samtools view -H "$1" | less' filecmd /abs/path/sample.bam sample.bam /abs/path
So a file named x; rm -rf ~ or $(curl evil|sh).bam is just an odd file
name. Before anything runs, the path must also be an existing file or folder
on the terminal's machine. And because the path is always absolute, a name
that begins with - can't be taken as an option.
Write placeholders bare, as in less {path}. '{path}' and "{path}" also
work, because the quotes are absorbed. Your own command text is trusted as
written: it's your config.
The copied path is passed to your command as data, never spliced into the shell text, so a file name can't inject commands. The path must also exist on the terminal's machine first. That's all this plugin guarantees:
- Your command templates are trusted as written. Don't write ones that
execute their argument, such as
sh -c {path},source {path},eval, orxargs sh -c. Quoting can't protect a command that treats the path as a program. - The file's contents are not checked. Viewing an untrusted file with
catcan send escape sequences to your terminal. Preferless, which shows them as text unless yourLESSvariable includes-r, or pipe throughcat -v. Commands that interpret their input, such asmake -f,Rscript, or aLESSOPENpreprocessor, run what's in the file. interactive = trueruns your shell startup files before the command.- The file can change between the check and the run. That matters only if someone else can write to its folder.
Provided as is, under the MIT license, with no warranty.
The clipboard is read on the machine that runs the plugin. Rex for Mac runs
actions from the Mac's own ~/.config/rex/init.lua, even when the focused
session is on a remote host, so install it on your Mac. The floating pane
and the command still run on the terminal's own machine, local or remote,
where the file is.
The plugin is one file. Save it beside your init.lua, where Rex's
require looks for modules:
curl -fsSL -o ~/.config/rex/filecmd.lua \
https://raw.githubusercontent.com/williamrowell/rex-filecmd/main/filecmd.luaThen add this to ~/.config/rex/init.lua:
require("filecmd").setup() -- the built-in examples onlyor, with commands of your own:
require("filecmd").setup{
commands = { bamheader = "samtools view -H {path} | less" },
}Then reload the configuration so the shortcut takes effect: choose
Reload Config in the command palette, or run rex config reload. Reload
again after changing commands or updating the file. To update, run the same
curl again.
If you set XDG_CONFIG_HOME, your configuration is in
$XDG_CONFIG_HOME/rex/ instead, so save the file there.
require("filecmd").setup{
key = "alt+cmd+o", -- false leaves it unbound
commands = {}, -- see above
examples = true, -- false drops the built-in examples
bounds = { 0.08, 0.08, 0.84, 0.84 }, -- pane x, y, width, height, as fractions of the window
interactive = false, -- default for commands that don't set it
cwd = "file", -- "file": the file's folder; "terminal": the terminal's cwd
}Option+Cmd+O isn't one of Rex for Mac's or macOS's default shortcuts.
The action takes optional path and command arguments, so scripts can skip
the clipboard and the picker:
rex do filecmd_run path=reads.bam command=bamheaderrex do test/unit.lua # placeholders, hostile file names, hold, merging
# end to end; opens a small floating pane that closes itself:
rex do test/smoke-init.lua --action filecmd_run -s SESSION -b BLOCK path=/some/file command=probe --json