A Custom Vim Mapping to Copy a Buffer's File Path to Your Clipboard
Sometimes quickly copying a file's relative path is exactly what you need. Here's one way to do that.
You can use this for a lot of things but my primary use case is around cross linking blog posts on this site which uses Jekyll (a static site generator).
Jekyll provides a {% post_url %}
template tag where you pass in the relative
file path of the post you want to link. Whenever I want to cross link a post I
open the file, quickly hit this new cp
mapping and paste it into the tag’s
value with a minor amount of touching up.
Here’s the mapping: nmap cp :let @+ = expand("%")<CR>
.
The video below goes into a bit more details on how it works and how I’m using it.
# Demo Video
Timestamps
- 0:10 – Demonstrating 1 potential use case with Jekyll
- 1:33 – Going over the Vim mapping in my vimrc file
Reference Links
How do you quickly copy file paths in Vim? Let me know below.