Skip to content

vieitesss/command.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

command.nvim

Neovim plugin that allows you to:

  • Type a command you want to run and execute it directly in a terminal inside Neovim.
    • You can use your shell configuration (e.g., aliases, functions).
  • Keep a history of executed commands and easily access them.
  • Re-execute the last executed command with a single command.
  • Search through the history of executed commands using:
    • fzf-lua
  • Use context variables in your commands (e.g., ${file}, ${line}, ${selection}) to reference the current editor state.
  • Get completion suggestions as virtual text while typing.
  • Validate commands for potentially dangerous patterns (command substitution, piping to rm/dd, redirects to system files, etc.) with optional warnings and confirmation prompts.
  • Follow compilation errors for some languages (credits to compile-mode.nvim).

Installation

vim.pack

vim.pack.add({
    { src = "https://github.com/vieitesss/command.nvim" }
})

require('command').setup()

lazy.nvim

return {
    "vieitesss/command.nvim",
    lazy = false,
    opts = true,
}

Configuration

local prompt_act = require 'command.actions.prompt'
local terminal_act = require 'command.actions.terminal'
defaults = {
    history = {
        max = 200,
        picker = "fzf-lua"
    },
    ui = {
        prompt = {
            max_width = 40,
            ghost_text = true
        },
        terminal = {
            height = 0.25,
            split = "below"
        }
    },
    validation = {
        warn = true  -- Show warnings for potentially dangerous patterns
    },
    keymaps = {
        prompt = {
            -- WARNING: There is no `i` in prompt mode
            ni = { -- normal and insert modes
                { '<Up>', prompt_act.history_up },
                { '<Down>', prompt_act.history_down },
                { '<C-f>', prompt_act.search },
                { '<CR>', prompt_act.enter },
                { '<C-d>', prompt_act.cancel },
                { '<C-e>', prompt_act.accept_ghost },
            },
            n = { -- normal mode
                { '<Esc>', prompt_act.cancel }
            }
        },
        terminal = {
            -- WARNING: There is no `ni` in terminal mode
            n = {
                { '<CR>', terminal_act.follow_error }
            }
        }
    }
}

How to use

The plugin provides you two commands:

  • CommandExecute: Opens a prompt and asks you for the command that you want to execute. Then, a terminal appears and runs the command.
  • CommandExecuteLast: Runs the last executed command. CommandExecute must have been called previously during the session.
  • CommandExecuteSelection: Executes the current text selection as a shell command directly.

Context Variables

You can use the following variables in your commands to reference the current editor context. They will be automatically expanded before execution.

  • ${file}: Absolute path of the current buffer.
  • ${fileDir}: Directory of the current buffer (:h).
  • ${fileName}: Filename of the current buffer (:t).
  • ${fileRoot}: Filename without extension (:r).
  • ${line}: Current cursor line number.
  • ${col}: Current cursor column number.
  • ${cwd}: Current working directory.
  • ${selection}: The text from the last visual selection.
  • ${selection:sh}: The text from the last visual selection, shell-escaped for safe use in commands.

Raw vs Escaped Selection

  • Use ${selection} (Raw) when the selection contains multiple arguments, flags, or shell operators that you want the shell to interpret.
    • Example: rm ${selection} where selection is file1.txt file2.txt.
    • Example: grep ${selection} where selection is -r "search" .
  • Use ${selection:sh} (Escaped) when the selection is a single string containing special characters (spaces, quotes, &, etc.) that should be treated as a single argument.
    • Example: git commit -m ${selection:sh} where selection is Fix: "broken" logic & typos.
    • Example: curl ${selection:sh} where selection is https://example.com?query=1&param=2.

Examples:

  • Run the current file: python ${file}
  • Git blame current line: git blame -L ${line},+1 ${file}
  • Echo selected text: echo '${selection}'
  • Search selected text (even with special characters) with grep: grep ${selection:sh} ${file}

Validation Warnings

The plugin validates commands for potentially dangerous patterns and shows a warning before execution. This helps prevent accidental execution of destructive commands.

Detected Patterns

The plugin warns about:

  • Command substitution: $(...) or `...`
  • Piping to dangerous commands: | rm, | dd, | mkfs, | shred
  • Redirects to system files: > /etc/, > /sys/, > /dev/
  • Background execution: & at the end
  • Command chains: ||, &&

Disabling Warnings

If you want to disable validation warnings, set validation.warn = false:

require('command').setup({
    validation = {
        warn = false
    }
})

When warnings are enabled (default), the plugin will display the dangerous pattern(s) detected and the full command, then prompt you to confirm execution with (y/n):.

About

Run commands in a builtin Neovim terminal and follow compilation errors

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published