Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yank file path with line no from vim to system clipboard

I would like to yank -

  • A full path to the file, e.g. c:\foo\bar\file.txt:94 with its line no

I would also like to paste it to my system clipboard so i will use '+' register for this.

can you suggest me possible way to do this?

like image 583
Hardik Juneja Avatar asked Jul 05 '13 23:07

Hardik Juneja


People also ask

How do I copy a line from VI to Notepad?

Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.

How do I copy text from vi editor to clipboard?

How to Copy to System's Clipboard. In Vim, you can copy, cut, and paste texts using the yank commands provided by the y, d, and p keys, respectively. However, by default, vim will store the copied text into an internal register called the unnamed register.


1 Answers

If you are trying to yank c:\foo\bar\file.txt:94 when you are on line 94 of c:\foo\bar\file.txt you can use the following statement to set the + register to
<file_path>:<line_number>

:let @+=expand("%") . ':' . line(".")

expand("%") - is the current file name
line(".") - is the current line number

An example mapping is

nnoremap <leader>y :let @+=expand("%") . ':' . line(".")<CR>
like image 185
FDinoff Avatar answered Sep 22 '22 17:09

FDinoff