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).
vim.pack.add({
{ src = "https://github.com/vieitesss/command.nvim" }
})
require('command').setup()return {
"vieitesss/command.nvim",
lazy = false,
opts = true,
}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 }
}
}
}
}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.CommandExecutemust have been called previously during the session.CommandExecuteSelection: Executes the current text selection as a shell command directly.
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.
- 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 isfile1.txt file2.txt. - Example:
grep ${selection}where selection is-r "search" .
- Example:
- 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 isFix: "broken" logic & typos. - Example:
curl ${selection:sh}where selection ishttps://example.com?query=1¶m=2.
- Example:
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}
The plugin validates commands for potentially dangerous patterns and shows a warning before execution. This helps prevent accidental execution of destructive commands.
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:
||,&&
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):.