xplr allows you to map actions directly to key bindings in different modes, and so, it doesn't come with a built-in command mode. However, being insanely hackable, it doesn't stop you from implementing your own.
There's already a plugin that implements command mode in an interesting way - command-mode.xplr.
Install and set up the plugin. Follow the official instruction.
local m = require("command-mode")
m.setup()
By default, this plugin captures the :
key in default mode and moves the action mode to ;
key, so that you can intuitively enter :command
to execute the commands.
So, let's try creating some basic commands.
A command to create a file (BASH example)
local create_file = m.cmd("create-file", "Create File")(m.BashExec([===[
read -p "Enter file path: " path
touch "$path"
# Focus on the created file
"$XPLR" -m ExplorePwd
"$XPLR" -m "FocusPath: %q" "$path"
]===]))
A command to create a directory (Lua example)
local create_dir = m.cmd("create-dir", "Create Directory")(function(app)
io.write("Enter directory path: ")
io.flush()
local path = io.read()
xplr.util.shell_execute("mkdir", { path })
-- Focus on the created directory
return {
"ExplorePwd",
{ FocusPath = path },
}
end)
Now type :create-file
or :create-dir
and press enter
. You will notice that xplr will suggest the matching commands interactively and also, autocomplete when pressing tab
. It will also let you navigate the command history by pressing up
and down
arrow keys.
With this knowledge, now you can define custom commands for every operation you want to perform in xplr, without having to remember which key is mapped to which operation.
However, you may still want to bind your most used commands to keys to perform quick actions. In that case, just use the .bind()
function like below.
Bind commands to keys
create_file.bind("default", "f")
-- Or
create_dir.bind(xplr.config.modes.builtin.default, "D")
Now, you should be able to press f
to execute :create-file
and press D
to execute :create-dir
.
More Options
If the operation you want to perform is non-interactive, i.e. it doesn't prompt you for input, you may want to perform it silently, i.e. without the flickering of screen. In that case, use m.silent_cmd
instead of m.cmd
and m.BashExecSilently
instead of m.BashExec
.
Write a comment ...