vimtricks.wiki Concise Vim tricks, one at a time.

How do I copy the current file's full path to the system clipboard?

Answer

:let @+ = expand('%:p')

Explanation

Sometimes you need to share or use the full path of the file you're editing — for a terminal command, a config file, or a chat message. This command copies the absolute path directly to your system clipboard so you can paste it anywhere.

How it works

  • :let @+ — assigns a value to the + register, which is the system clipboard
  • expand('%:p') — expands the current file's name (%) with the :p modifier to produce the full absolute path

The result is something like /home/user/project/src/main.go placed directly on your clipboard.

Example

If you're editing src/main.go from the project root:

:let @+ = expand('%:p')

Your clipboard now contains:

/home/user/project/src/main.go

Paste it anywhere with Ctrl+V in your terminal or another application.

Tips

  • Use expand('%:t') for just the filename (main.go)
  • Use expand('%:p:h') for just the directory (/home/user/project/src)
  • Use expand('%:.') for the path relative to the working directory (src/main.go)
  • Use @* instead of @+ if your system uses the primary selection (middle-click paste on Linux)
  • Map it for quick access: :nnoremap <leader>cp :let @+ = expand('%:p')<CR>

Next

How do I return to normal mode from absolutely any mode in Vim?