Skip to content

Instantly share code, notes, and snippets.

@tomaszkubacki
Created October 1, 2025 15:55
Show Gist options
  • Select an option

  • Save tomaszkubacki/30ad3cd9db24131141e27434230e4b61 to your computer and use it in GitHub Desktop.

Select an option

Save tomaszkubacki/30ad3cd9db24131141e27434230e4b61 to your computer and use it in GitHub Desktop.
Lazy vim keymaps.lua mapping to execute current line in terminal
-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
-- Add any additional keymaps here
--
local map = LazyVim.safe_keymap_set
function execute_line()
-- Yank the current line into the default register
vim.cmd('normal! ""yy')
local text = vim.fn.getreg('"')
local snacks_terminal = require("snacks.terminal")
local terminal = snacks_terminal.get()
if not terminal then
terminal = snacks_terminal.open()
end
local term_buf = terminal and terminal.buf
local term_win = terminal and terminal.win
-- Only call :show() if the window is NOT already open
local win_valid = term_win and vim.api.nvim_win_is_valid(term_win)
if not win_valid then
terminal:show()
-- refresh window handle
term_win = terminal.win
end
if term_buf and vim.api.nvim_buf_is_valid(term_buf) then
vim.schedule(function()
if term_win and vim.api.nvim_win_is_valid(term_win) then
vim.api.nvim_set_current_win(term_win)
end
vim.api.nvim_set_current_buf(term_buf)
vim.cmd("startinsert")
vim.api.nvim_feedkeys(text, "t", false)
vim.api.nvim_feedkeys("\r", "t", false) -- Enter
end)
else
vim.notify("Could not find or open terminal.", vim.log.levels.ERROR)
end
end
map("n", "<leader>te", function()
execute_line()
end, { desc = "execute in terminal" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment