Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Last active September 1, 2024 17:59
Show Gist options
  • Save aneurysmjs/03c581d7b96d27c50aa7a636eefa6c4c to your computer and use it in GitHub Desktop.
Save aneurysmjs/03c581d7b96d27c50aa7a636eefa6c4c to your computer and use it in GitHub Desktop.

copy relative file to clipboard

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.

Key combinators

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment