Giter VIP home page Giter VIP logo

command.nvim's Introduction

command.nvim

command.nvim is a simple command runner. You give it a command and it runs it in a new or existing pane of your multiplexer (or a ToggleTerm terminal). It remembers the last command so it can repeat it. It can run the current file as is or a custom command depending on rules (see Rules section). If the current file is not executable, it asks if you want to make it executable.

It supports the following multiplexers/backends:

  • tmux
  • wezterm
  • toggleterm

Installing

With Lazy.nvim

{
    'cultab/command.nvim',
    opts = {},
    dependencies = {
        'MunifTanjim/nui.nvim',
        'akinsho/toggleterm.nvim' -- optional, for the toggleterm backend
    }
}

Usage

Setup keymaps for each action that you want to use. For example using lua:

local map = vim.keymap.set
map('n','<leader>ct', require('command').change_direction, { desc = '[c]ommand [t]oggle pane direction' })
map('n','<leader>cc', require('command').run_command,      { desc = '[c]ommand shell [c]ommand'         })
map('n','<leader>cr', require('command').run_current_file, { desc = '[c]ommand [r]un current file'      })
map('n','<leader>cl', require('command').run_last_command, { desc = '[c]ommand repeat [l]ast command'   })

User Commands

You can also use the following user commands instead of the exported lua functions.

  • :CommandChangeDirection Toggles pane direction for running commands

  • :CommandRun Show prompt to ask for a command to run

  • :CommandFile Runs current file

  • :CommandLast Runs last command

Multiplexers & Backends

tmux & wezterm

The tmux and wezterm backends both have 2 built in pane directions, right of the editor pane, and below the editor pane.

ToggleTerm

The ToggleTerm backend does not support toggling different pane directions, it uses the direction configured in ToggleTerm's setup.

Configuration

Configurations is done by passing opts to the setup function.

require('command.nvim').setup( {--[[ your options here ]]} )

Backends

The backend is the multiplexer or terminal to use. It's controlled by the use key in the opts table.

opts = {
    --- the backend to use, one of:
    --- @alias backend_used
    --- | 'auto' -- pick automatically by examining environment vars
    --- | 'tmux'
    --- | 'wezterm'
    --- | 'toggleterm'
	use = "auto",
}

Rules

Rules are key-value pairs of lua patterns and functions. They are passed into the setup() function through the opts table.

opts = {
	rules = {
        -- run the current file with `nvim -l` if it ends with '.lua'
		[".*%.lua"] = function(filepath)
			return "nvim -l " .. filepath
		end,
        -- run the default Makefile rule if the current file is called 'Makefile'
		["Makefile"] = function(_)
			return "make"
		end,
	},
}

The lua pattern is matched against the current filename. The function must accept an optional argument and return a string. The optional argument will contain the filepath to the current file. The return value will be the shell command to be run when the name of the current file matches the pattern.

Default Opts

The defaults options are as follows:

local default_opts = {
    --- the backend to use, one of:
    --- @alias backend_used
    --- | 'auto' -- pick automatically by examining environment vars
    --- | 'tmux'
    --- | 'wezterm'
    --- | 'toggleterm'
	use = "auto",

    --- defines rules to overwrite the command to run when using the "run current file" behavior
    --- keys are lua patterns (see :help lua-pattern)
    ---
    --- values are functions that accept:
    ---     an optional argument containing the path to the current file
    --- returns:
    ---     a string of the shell command to run
    --- @alias rule table<string, fun(string?):string>
	rules = {
        -- run the current file with `nvim -l` if it ends with '.lua'
		[".*%.lua"] = function(filepath)
			return "nvim -l " .. filepath
		end,
        -- run the default Makefile rule if the current file is called 'Makefile'
		["Makefile"] = function(_)
			return "make"
		end,
	},

    --- whether to check if keys in the opts passed to setup are valid
    --- @type bool
	validate = true,

    --- an icon to use for prompts and notifications
    --- @type string
	icon = "$ ",
}

TODO

  • slime-like behaviors
    • send current line to pane
    • send buffer contents to pane
    • send virtual selection to pane
    • send buffer contents up to current line to pane
  • backends
    • tmux
    • ToggleTerm
    • wezterm
    • default nvim terminal
    • kitty
    • some other backend
    • zellij
  • clean up types
    • types for all opts, use lua-ls enums
    • move to utils ?
    • also in readme also
  • history instead of just last command
  • add bugs
  • user configured pane directions
    • maybe fully custom
    • maybe add every choice and make their availability configurable

command.nvim's People

Contributors

cultab avatar github-actions[bot] avatar

Stargazers

Knightly_ avatar Yuta Katayama avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.