environment/home/.config/nvim/lua/command_runner.lua

37 lines
845 B
Lua
Raw Normal View History

2025-08-08 07:55:29 +02:00
local M = {}
function M.run_shell_command_under_cursor()
local cmd = vim.api.nvim_get_current_line()
if cmd == "" then
print("No command on this line.")
return
end
-- Run command and capture output
local output = vim.fn.systemlist(cmd)
-- Save current window to return later
local current_win = vim.api.nvim_get_current_win()
-- Move to the right window (assumes it's already split)
vim.cmd("wincmd l")
-- Optional: clear buffer before writing
vim.api.nvim_buf_set_lines(0, 0, -1, false, {})
-- Set output in current buffer (right side)
vim.api.nvim_buf_set_lines(0, 0, -1, false, output)
-- Optional: make it editable plain text
vim.bo.filetype = "text"
vim.bo.modifiable = true
vim.bo.readonly = false
-- Return to the left window
vim.api.nvim_set_current_win(current_win)
end
return M