How to Yank the Buffer File Name in Neovim to Your Clipboard
Hi there! If you're a Neovim user like me and you frequently use AI-powered plugins like code-ai.nvim, you probably find yourself needing to yank file names into prompts or chats pretty often.
The Problem: Jumping Back and Forth to Yank File Names
When using code AI or other plugins to enhance your workflow, you might often need the exact filename—or path—of the buffer you're currently editing. Yanking file paths or names seems trivial at first, but it can quickly become cumbersome. Not to mention distracting. Typically, you’d have to exit Neovim or switch to another shell window just to remember the exact file or path you're editing. Needless to say, this interrupts the smooth workflow that makes using Neovim enjoyable in the first place.
The Solution: Neovim Keybindings to the Rescue!
I grew tired of having to pause my workflow just to copy the filename or path and decided to streamline the process. My solution was to create custom Neovim keybindings using the leader key to quickly yank different variations of the file name and path directly into my clipboard.
I created four keybindings that allow you to quickly yank:
- The absolute path (
ya ) - The relative path (
yr ) - Filename only (
yf ) - Basename (filename without extension,
yb )
Here’s the Lua Code I Used
If you'd like to use these bindings too (I highly encourage it!), here's my implementation. All you have to do is add this to your Neovim configuration file—the one where you keep your key mappings, typically mappings.lua.
-- Yank absolute file path to clipboard
local function yank_absolute_file_path()
local file_path = vim.fn.expand(':p')
if file_path and file_path ~= '' then
vim.fn.setreg('+', file_path)
vim.notify("Copied absolute path: " .. file_path, vim.log.levels.INFO, { title = "File Path" })
else
vim.notify("No file path to copy", vim.log.levels.WARN, { title = "File Path" })
end
end
-- Yank relative file path to clipboard
local function yank_relative_file_path()
local file_path = vim.fn.expand(':.')
if file_path and file_path ~= '' then
vim.fn.setreg('+', file_path)
vim.notify("Copied relative path: " .. file_path, vim.log.levels.INFO, { title = "File Path" })
else
vim.notify("No file path to copy", vim.log.levels.WARN, { title = "File Path" })
end
end
-- Yank filename only to clipboard
local function yank_filename()
local filename = vim.fn.expand(':t')
if filename and filename ~= '' then
vim.fn.setreg('+', filename)
vim.notify("Copied filename: " .. filename, vim.log.levels.INFO, { title = "File Name" })
else
vim.notify("No filename to copy", vim.log.levels.WARN, { title = "File Name" })
end
end
-- Yank basename (filename without extension) to clipboard
local function yank_basename()
local basename = vim.fn.expand(':t:r')
if basename and basename ~= '' then
vim.fn.setreg('+', basename)
vim.notify("Copied basename: " .. basename, vim.log.levels.INFO, { title = "File Basename" })
else
vim.notify("No basename to copy", vim.log.levels.WARN, { title = "File Basename" })
end
end
-- File path yanking keybindings
map("n", "ya", yank_absolute_file_path, { desc = "Yank absolute file path" })
map("n", "yr", yank_relative_file_path, { desc = "Yank relative file path" })
map("n", "yf", yank_filename, { desc = "Yank filename" })
map("n", "yb", yank_basename, { desc = "Yank basename (filename without extension)" })
Result: A Happy, Efficient Workflow
Now, whenever you need to prompt your AI assistant (or any other tool), you can quickly copy absolute or relative paths, filenames, or basenames directly from your Neovim session. There's no more context-switching. It’s just you, your workflow, and your favorite tools, working smoothly and efficiently.
If you enjoy integrating Neovim with your workflow—especially with awesome AI-powered plugins like code-ai.nvim—these convenient keybindings can really streamline your process. Give it a try, and let me know how it works out for you or if you've got your own neat workflow tips to share!
Happy coding!