{"slug": "code-from-https-www-youtube-com-watch-v-n6tje-irxgc", "title": "Code from https://www.youtube.com/watch?v=n6TJe_IrxGc", "summary": "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.", "body_md": "| -- Copy file path / selection reference for pasting into AI chats | |\n| local function copy_ref(opts) | |\n| -- \"%\" is the current buffer's file name; \":.\" makes it relative to the cwd | |\n| local path = vim.fn.expand(\"%:.\") | |\n| -- ref is what ends up in the clipboard; start with just the path | |\n| local ref = path | |\n| if opts.visual then | |\n| -- '< and '> are only set after leaving visual mode, so read the live selection: | |\n| -- \"v\" is the line where visual mode was started (the anchor) | |\n| local start_line = vim.fn.line(\"v\") | |\n| -- \".\" is the line the cursor is on now (the moving end of the selection) | |\n| local end_line = vim.fn.line(\".\") | |\n| -- if the selection was made upward, swap so start is always the smaller line | |\n| if start_line > end_line then | |\n| start_line, end_line = end_line, start_line | |\n| end | |\n| -- append the range, e.g. \"lua/config/keymaps.lua:1:23\" | |\n| ref = path .. \":\" .. start_line .. \":\" .. end_line | |\n| end | |\n| -- ask for an optional free-text note on the command line (Enter to skip) | |\n| local note = vim.fn.input(\"Prompt (optional): \") | |\n| if note ~= \"\" then | |\n| -- append the note after the ref, separated by a space | |\n| ref = ref .. \" \" .. note | |\n| end | |\n| -- write ref into the \"+\" register, which is the system clipboard | |\n| vim.fn.setreg(\"+\", ref) | |\n| -- show a confirmation message with what was copied | |\n| vim.notify(\"Copied: \" .. ref) | |\n| end | |\n| -- normal mode: copy just the file path | |\n| vim.keymap.set(\"n\", \"<leader>cp\", function() | |\n| copy_ref({}) | |\n| end, { desc = \"Copy file path\" }) | |\n| -- visual mode: copy the file path plus the selected line range | |\n| vim.keymap.set(\"v\", \"<leader>cp\", function() | |\n| copy_ref({ visual = true }) | |\n| end, { desc = \"Copy file path with line range\" }) |\n\n| #!/usr/bin/env bash | |\n| set -euo pipefail | |\n| PROJECTS_DIR=\"$HOME/Projects\" | |\n| # Menu lines: display_label<TAB>session_name<TAB>path<TAB>type | |\n| menu=\"\" | |\n| # 1. Existing tmux sessions (skip worktree sessions, handled in section 3) | |\n| if tmux ls &>/dev/null; then | |\n| while IFS= read -r sess; do | |\n| [[ \"$sess\" == *\"(\"* ]] && continue | |\n| menu+=\"[switch] ${sess}\"$'\\t'\"${sess}\"$'\\t'\"$sess\"$'\\t'\"switch\"$'\\n' | |\n| done < <(tmux list-sessions -F '#{session_name}') | |\n| fi | |\n| # 2. Top-level projects not already a session | |\n| for dir in \"$PROJECTS_DIR\"/*/; do | |\n| [ -d \"$dir\" ] || continue | |\n| name=$(basename \"$dir\") | |\n| tmux has-session -t \"$name\" 2>/dev/null && continue | |\n| menu+=\"[new] ${name}\"$'\\t'\"${name}\"$'\\t'\"${dir}\"$'\\t'\"new\"$'\\n' | |\n| done | |\n| # 3. Worktrees inside each project's .worktrees/ | |\n| for dir in \"$PROJECTS_DIR\"/*/; do | |\n| { [ -d \"$dir/.git\" ] || [ -f \"$dir/.git\" ]; } || continue | |\n| (cd \"$dir\" && git worktree prune 2>/dev/null) || true | |\n| wt_dir=\"${dir}.worktrees\" | |\n| [ -d \"$wt_dir\" ] || continue | |\n| project_name=$(basename \"$dir\") | |\n| for branch_dir in \"$wt_dir\"/*/; do | |\n| [ -d \"$branch_dir\" ] || continue | |\n| branch_name=$(basename \"$branch_dir\") | |\n| sess_name=\"${project_name}(${branch_name})\" | |\n| if tmux has-session -t \"$sess_name\" 2>/dev/null; then | |\n| label=\"[switch] ${project_name} (${branch_name})\" | |\n| type=\"switch\" | |\n| else | |\n| label=\"[new] ${project_name} (${branch_name}) [worktree]\" | |\n| type=\"new\" | |\n| fi | |\n| menu+=\"${label}\"$'\\t'\"${sess_name}\"$'\\t'\"${branch_dir}\"$'\\t'\"${type}\"$'\\n' | |\n| done | |\n| done | |\n| [ -z \"$menu\" ] && exit 0 | |\n| selection=$(printf '%s' \"$menu\" | LC_ALL=C sort -t$'\\t' -k4,4r -k1,1 | fzf --height 100% --delimiter=$'\\t' --with-nth=1) | |\n| [ -z \"$selection\" ] && exit 0 | |\n| sess=$(printf '%s' \"$selection\" | cut -f2) | |\n| path=$(printf '%s' \"$selection\" | cut -f3) | |\n| type=$(printf '%s' \"$selection\" | cut -f4) | |\n| if [ \"$type\" = \"switch\" ]; then | |\n| if [ -z \"${TMUX:-}\" ]; then | |\n| tmux attach -t \"$sess\" | |\n| else | |\n| tmux switch-client -t \"$sess\" | |\n| fi | |\n| else | |\n| if [ -z \"${TMUX:-}\" ]; then | |\n| tmux new-session -s \"$sess\" -c \"$path\" | |\n| else | |\n| tmux new-session -d -s \"$sess\" -c \"$path\" | |\n| tmux switch-client -t \"$sess\" | |\n| fi | |\n| fi |\n\n| #!/usr/bin/env bash | |\n| set -euo pipefail | |\n| PROJECTS_DIR=\"$HOME/Projects\" | |\n| project_dir=$(find \"$PROJECTS_DIR\" -mindepth 1 -maxdepth 1 -type d | fzf --height 100% --prompt=\"Project> \") | |\n| [ -z \"$project_dir\" ] && exit 0 | |\n| project_name=$(basename \"$project_dir\") | |\n| cd \"$project_dir\" | |\n| if [ ! -d .git ] && [ ! -f .git ]; then | |\n| echo \"Not a git repo: $project_dir\" | |\n| read -r -p \"Press enter to close...\" | |\n| exit 1 | |\n| fi | |\n| git worktree prune 2>/dev/null || true | |\n| branches=$(git branch --format='%(refname:short)') | |\n| selection=$(printf '%s\\n[new branch]\\n' \"$branches\" | fzf --height 100% --prompt=\"Branch> \") | |\n| [ -z \"$selection\" ] && exit 0 | |\n| if [ \"$selection\" = \"[new branch]\" ]; then | |\n| read -r -p \"New branch name: \" branch | |\n| [ -z \"$branch\" ] && exit 0 | |\n| is_new=true | |\n| else | |\n| branch=\"$selection\" | |\n| is_new=false | |\n| fi | |\n| wt_root=\"$project_dir/.worktrees\" | |\n| mkdir -p \"$wt_root\" | |\n| if ! grep -qxF '.worktrees/' .gitignore 2>/dev/null; then | |\n| echo '.worktrees/' >> .gitignore | |\n| fi | |\n| wt_path=\"$wt_root/$branch\" | |\n| existing_path=$(git worktree list --porcelain | awk -v b=\"refs/heads/$branch\" ' | |\n| /^worktree / { path=$2 } | |\n| $0 == \"branch \" b { print path } | |\n| ') | |\n| if [ -n \"$existing_path\" ]; then | |\n| wt_path=\"$existing_path\" | |\n| elif [ -d \"$wt_path\" ]; then | |\n| echo \"Directory exists but isn't a registered worktree: $wt_path\" | |\n| read -r -p \"Press enter to close...\" | |\n| exit 1 | |\n| else | |\n| if [ \"$is_new\" = true ]; then | |\n| git worktree add \"$wt_path\" -b \"$branch\" | |\n| else | |\n| git worktree add \"$wt_path\" \"$branch\" | |\n| fi | |\n| fi | |\n| sanitized_branch=$(echo \"$branch\" | tr '.:/ ' '----') | |\n| sess_name=\"${project_name}(${sanitized_branch})\" | |\n| tmux has-session -t \"$sess_name\" 2>/dev/null || tmux new-session -d -s \"$sess_name\" -c \"$wt_path\" | |\n| if [ -z \"${TMUX:-}\" ]; then | |\n| tmux attach -t \"$sess_name\" | |\n| else | |\n| tmux switch-client -t \"$sess_name\" | |\n| fi |", "url": "https://wpnews.pro/news/code-from-https-www-youtube-com-watch-v-n6tje-irxgc", "canonical_source": "https://gist.github.com/smnatale/b30dc21ff330495641fb59f36005562c", "published_at": "2026-07-17 09:06:37+00:00", "updated_at": "2026-07-23 20:57:17.172680+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Neovim", "tmux", "fzf", "git"], "alternates": {"html": "https://wpnews.pro/news/code-from-https-www-youtube-com-watch-v-n6tje-irxgc", "markdown": "https://wpnews.pro/news/code-from-https-www-youtube-com-watch-v-n6tje-irxgc.md", "text": "https://wpnews.pro/news/code-from-https-www-youtube-com-watch-v-n6tje-irxgc.txt", "jsonld": "https://wpnews.pro/news/code-from-https-www-youtube-com-watch-v-n6tje-irxgc.jsonld"}}