new
This commit is contained in:
@@ -29,6 +29,39 @@ vim.api.nvim_create_user_command('MessagesToBuffer', function()
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
end, { desc = 'Open :messages output in a scratch buffer' })
|
||||
|
||||
vim.api.nvim_create_user_command('CmdToBuffer', function(opts)
|
||||
local cmd = table.concat(opts.fargs, ' ')
|
||||
|
||||
vim.g.__cmd_to_buffer_cmd = cmd
|
||||
vim.g.__cmd_to_buffer_out = nil
|
||||
|
||||
local exec_line = opts.bang and 'silent execute' or 'execute'
|
||||
local ok, err = pcall(vim.cmd, string.format(
|
||||
[[
|
||||
redir => g:__cmd_to_buffer_out
|
||||
%s g:__cmd_to_buffer_cmd
|
||||
redir END
|
||||
]],
|
||||
exec_line
|
||||
))
|
||||
if not ok then
|
||||
pcall(vim.cmd, 'redir END')
|
||||
error(err)
|
||||
end
|
||||
|
||||
local output = vim.g.__cmd_to_buffer_out or ''
|
||||
vim.g.__cmd_to_buffer_cmd = nil
|
||||
vim.g.__cmd_to_buffer_out = nil
|
||||
local lines = vim.split(output, '\n', { plain = true, trimempty = false })
|
||||
|
||||
vim.cmd('new')
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
vim.bo[bufnr].buftype = 'nofile'
|
||||
vim.bo[bufnr].bufhidden = 'wipe'
|
||||
vim.bo[bufnr].swapfile = false
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
end, { nargs = '+', bang = true, complete = 'command', desc = 'Run an Ex command and open its output in a scratch buffer' })
|
||||
|
||||
-- Automatically create a scratch buffer if Neovim starts with no files
|
||||
au('VimEnter', {
|
||||
group = group,
|
||||
|
||||
Reference in New Issue
Block a user