Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 19, 2022 07:00 am GMT

Forget key bindings and get yourself a command mode

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  echo ExplorePwd >> "$XPLR_PIPE_MSG_IN"  echo FocusPath: "'"$path"'" >> "$XPLR_PIPE_MSG_IN"]===]))

A command to create a directory (Lua example)

local create_dir = m.cmd("create-dir", "Create Directory")(function()  io.write("Enter directory path: ")  io.flush()  local path = io.read()  os.execute(string.format("mkdir %q", 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")-- Orcreate_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.

Follow xplr.stck.me for more xplr tutorial and tips.


Original Link: https://dev.to/sayanarijit/forget-key-bindings-and-get-yourself-a-command-mode-4j4g

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To