Skip to content

Instantly share code, notes, and snippets.

@Velrok
Created November 20, 2025 22:03
Show Gist options
  • Select an option

  • Save Velrok/1b779dea190dbfc1f56c8e472cbd80ad to your computer and use it in GitHub Desktop.

Select an option

Save Velrok/1b779dea190dbfc1f56c8e472cbd80ad to your computer and use it in GitHub Desktop.
-- Shared function to create GitHub gists
local function create_gist(opts, is_public)
local lines
local filetype = vim.bo.filetype
-- Check if command was called with a range (visual selection)
if opts.range > 0 then
-- Get visual selection
lines = vim.fn.getline(opts.line1, opts.line2)
else
-- Get entire buffer
lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
end
-- Create a temporary file with appropriate extension
local tmpfile = vim.fn.tempname()
if filetype ~= "" then
tmpfile = tmpfile .. "." .. filetype
end
-- Write lines to temporary file
vim.fn.writefile(lines, tmpfile)
-- Create the gist with appropriate visibility
local visibility_flag = is_public and "--public" or "--private"
local cmd = "gh gist create " .. visibility_flag .. " " .. tmpfile
vim.cmd("vsplit term://" .. cmd)
end
-- Create a private GitHub gist from visual selection or entire file
vim.api.nvim_create_user_command("GistCreate", function(opts)
create_gist(opts, false)
end, { range = true, desc = "Create a private GitHub gist from visual selection or entire file" })
-- Create a public GitHub gist from visual selection or entire file
vim.api.nvim_create_user_command("GistCreatePublic", function(opts)
create_gist(opts, true)
end, { range = true, desc = "Create a public GitHub gist from visual selection or entire file" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment