Skip to content

sayanarijit/command-mode.xplr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

command-mode-xplr.mp4

command-mode.xplr

This plugin acts like a library to help you define custom commands to perform actions.

Why

xplr has no concept of commands. By default, it requires us to map keys directly to a list of messages. While for the most part this works just fine, sometimes it gets difficult to remember which action is mapped to which key inside which mode. Also, not every action needs to be bound to some key.

In short, sometimes, it's much more convenient to define and enter commands to perform certain actions than trying to remember key bindings.

Installation

Install manually

  • Add the following line in ~/.config/xplr/init.lua

    local home = os.getenv("HOME")
    package.path = home
    .. "/.config/xplr/plugins/?/init.lua;"
    .. home
    .. "/.config/xplr/plugins/?.lua;"
    .. package.path
  • Clone the plugin

    mkdir -p ~/.config/xplr/plugins
    
    git clone https://github.com/sayanarijit/command-mode.xplr ~/.config/xplr/plugins/command-mode
  • Require the module in ~/.config/xplr/init.lua

    require("command-mode").setup()
    
    -- Or
    
    require("command-mode").setup{
      mode = "default",
      key = ":",
      remap_action_mode_to = {
        mode = "default",
        key = ";",
      }
    }
    
    -- Type `:` to enter command mode

Usage

Examples are taken from here and here.

-- Assuming you have installed and setup the plugin

local m = require("command-mode")

-- Setup with default settings
m.setup()

-- Type `:hello-lua` and press enter to know your location
local hello_lua = m.cmd("hello-lua", "Enter name and know location")(function(app)
  print("What's your name?")

  local name = io.read()
  local greeting = "Hello " .. name .. "!"
  local message = greeting .. " You are inside " .. app.pwd

  return {
    { LogSuccess = message },
  }
end)

-- Type `:hello-bash` and press enter to know your location
local hello_bash = m.silent_cmd("hello-bash", "Enter name and know location")(
  m.BashExec [===[
    echo "What's your name?"

    read name
    greeting="Hello $name!"
    message="$greeting You are inside $PWD"

    "$XPLR" -m "LogSuccess: %q" "$message"
  ]===]
)

-- Bind `:hello-lua` to key `h`
hello_lua.bind("default", "h")
-- or xplr.config.modes.builtin.default.key_bindings.on_key.h = hello_lua.action

-- Bind `:hello-bash` to key `H`
hello_bash.bind(xplr.config.modes.builtin.default, "H")
-- or xplr.config.modes.builtin.default.key_bindings.on_key.H = hello_bash.action

NOTE: To define non-interactive commands, use silent_cmd to avoid the flickering of screen.

Features

  • Tab completion
  • Command history navigation
  • Press ? to list commands
  • Press ! to spawn shell
  • Easily map keys to commands
  • Shortcut for BashExec and BashExecSilently messages.
  • Interactive UI