Skip to main content

Neovim Yank Filename

How to Yank the Buffer File Name in Neovim to Your Clipboard

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!

Popular posts from this blog

Undefined global vim

Defining vim as global outside of Neovim When developing plugins for Neovim, particularly in Lua, developers often encounter the "Undefined global vim" warning. This warning can be a nuisance and disrupt the development workflow. However, there is a straightforward solution to this problem by configuring the Lua Language Server Protocol (LSP) to recognize 'vim' as a global variable. Getting "Undefined global vim" warning when developing Neovim plugin While developing Neovim plugins using Lua, the Lua language server might not recognize the 'vim' namespace by default. This leads to warnings about 'vim' being an undefined global variable. These warnings are not just annoying but can also clutter the development environment with unnecessary alerts, potentially hiding other important warnings or errors. Defining vim as global in Lua LSP configuration to get rid of the warning To resolve the "Undefined global vi...

wget maven ntlm proxy

How to make wget, curl and Maven download behind an NTLM Proxy Working on CentOS, behind an NTLM proxy: yum can deal without problem with a NTLM Proxy wget, curl and Maven cannot The solution is to use " cntlm ". " cntlm " is a NTLM client for proxies requiring NTLM authentication. How it works Install "cntlm" Configure "cntlm"  by giving it your credentials by giving it the NTLM Proxy Start "cntlm" deamon (it listens to "127.0.0.1:3128") Configure wget, curl and Maven to use "cntlm" instead of using directly the NTLM Proxy Note: You will have then a kind of 2 stages Proxy : cntlm + the NTLM proxy Configure CNTLM After installing cntlm, the configuration file is in "cntlm.conf". You must have your domain (in the Windows meaning), proxy login and  proxy password. Mine are respectively: rktmb.org, mihamina, 1234abcd (yes, just for the example) You must have you NTLM Proxy Hostnama or IP ...

npm run build base-href

Using NPM to specify base-href When building an Angular application, people usually use "ng" and pass arguments to that invocation. Typically, when wanting to hard code "base-href" in "index.html", one will issue: ng build --base-href='https://ngx.rktmb.org/foo' I used to build my angular apps through Bamboo or Jenkins and they have a "npm" plugin. I got the habit to build the application with "npm run build" before deploying it. But the development team once asked me to set the "--base-href='https://ngx.rktmb.org/foo'" parameter. npm run build --base-href='https://ngx.rktmb.org/foo did not set the base href in indext.html After looking for a while, I found https://github.com/angular/angular-cli/issues/13560 where it says: You need to use −− to pass arguments to npm scripts. This did the job! The command to issue is then: npm run build -- --base-href='https://ngx.rktmb.org/foo...