How do I store the current file path in a named Vim register?
Answer
:let @a = expand('%:p')
Explanation
Named registers are not only for yanked text. You can store computed strings in them and paste later in Normal, Insert, or command-line mode. A practical use is saving the current file path into a register so you can quickly inject it into commit messages, logs, shell commands, or template text without retyping.
How it works
:letassigns a value in Vimscript@atargets registeraexpand('%:p')returns the current buffer's absolute file path
After running this once, you can paste the path with "ap in Normal mode or <C-r>a in Insert mode. This keeps your unnamed register untouched, so regular yank/paste flow stays clean.
Example
If the current buffer is:
/home/finn/work/vimtricks/internal/server/handlers.go
Run:
:let @a = expand('%:p')
Now insert the path anywhere with:
<C-r>a
Tips
- Use
%:tinstead of%:pif you only want the filename - Use another register (for example
@z) to avoid clashing with your macro register - Combine with
fnameescape()when you need a filename-safe form for Vim commands