To copy the relative path in Vim, you can use the following command:
:let @+=expand("%:.")
Here's how it works:
expand("%:.")
gets the current file's path relative to the current working directory.:let @+=
copies the result into the+
register, which is the system clipboard.
You can also map this command to a key in your Vim configuration file (~/.vimrc
or ~/.config/nvim/init.vim
for Neovim) for easier access:
nnoremap <leader>cp :let @+=expand("%:.")<CR>
In this example, <leader>cp
is the key mapping. You can replace <leader>cp
with any key combination you prefer.
Alternatively, if you have the vim-fugitive plugin installed, you can use the :Gread
command to get the relative path:
:Gread
Then, use "+y
to copy the path into the system clipboard.
n Vim (and Neovim), the notation refers to a key combination involving the “Meta” key (often mapped to the Alt key) and the letter h. Here’s how you should interpret it:
• <M> stands for the Meta key, which is usually the Alt key on most keyboards.
• h is the letter you press in combination with the Meta key.
Therefore, means you should press Alt (or Meta) and h simultaneously.
Common Modifier Keys Notations in Vim
• <C-...>: Control key (e.g., <C-w> means Ctrl + w).
• <M-...>: Meta key (usually the Alt key) (e.g., <M-h> means Alt + h).
• <S-...>: Shift key (e.g., <S-Tab> means Shift + Tab).
• <A-...>: Another notation sometimes used for the Alt key.
Examples
• <M-h>: Press Alt and h together.
• <M-j>: Press Alt and j together.
• <C-w>: Press Ctrl and w together.
• <S-Tab>: Press Shift and Tab together.