This is useful when executing lines that produce no output, so I have some confidence that something actually happened when pressing the key bindings. I am printing the message before executing the command because otherwise this feedback would shadow the output of the command being executed (which I am presumably interested in and would serve itself as feedback).
22 lines
740 B
Lua
22 lines
740 B
Lua
vim.bo.tabstop = 2
|
|
|
|
local buffer = { buffer = true }
|
|
|
|
local exec_current_lua_line = function()
|
|
local lineno = vim.fn.line('.')
|
|
print('Executing line ' .. lineno)
|
|
vim.fn.luaeval(vim.fn.getline(lineno))
|
|
end
|
|
|
|
local exec_current_lua_selection = function()
|
|
local selection = { vim.fn.line('v'), vim.fn.line('.') }
|
|
local first, last = vim.fn.min(selection), vim.fn.max(selection)
|
|
local code = vim.fn.join(vim.fn.getline(first, last), '\n')
|
|
print('Executing lines ' .. first .. ' to ' .. last)
|
|
loadstring(code)()
|
|
end
|
|
|
|
vim.keymap.set('n', '<leader>x', exec_current_lua_line, buffer)
|
|
vim.keymap.set('x', '<leader>x', exec_current_lua_selection, buffer)
|
|
vim.keymap.set('n', '<leader><leader>x', '<cmd>write | luafile %<cr>', buffer)
|
|
|