Code from https://www.youtube.com/watch?v=n6TJe_IrxGc A developer shared a Neovim Lua function that copies a file path and optional line range to the clipboard, along with a Bash script that uses fzf to switch or create tmux sessions based on project directories and git worktrees. | -- Copy file path / selection reference for pasting into AI chats | | | local function copy ref opts | | | -- "%" is the current buffer's file name; ":." makes it relative to the cwd | | | local path = vim.fn.expand "%:." | | | -- ref is what ends up in the clipboard; start with just the path | | | local ref = path | | | if opts.visual then | | | -- '< and ' are only set after leaving visual mode, so read the live selection: | | | -- "v" is the line where visual mode was started the anchor | | | local start line = vim.fn.line "v" | | | -- "." is the line the cursor is on now the moving end of the selection | | | local end line = vim.fn.line "." | | | -- if the selection was made upward, swap so start is always the smaller line | | | if start line end line then | | | start line, end line = end line, start line | | | end | | | -- append the range, e.g. "lua/config/keymaps.lua:1:23" | | | ref = path .. ":" .. start line .. ":" .. end line | | | end | | | -- ask for an optional free-text note on the command line Enter to skip | | | local note = vim.fn.input "Prompt optional : " | | | if note ~= "" then | | | -- append the note after the ref, separated by a space | | | ref = ref .. " " .. note | | | end | | | -- write ref into the "+" register, which is the system clipboard | | | vim.fn.setreg "+", ref | | | -- show a confirmation message with what was copied | | | vim.notify "Copied: " .. ref | | | end | | | -- normal mode: copy just the file path | | | vim.keymap.set "n", "