1

I would like to yank the current selected line range (not the line range content).

For example, if my selection goes from line 51 to 67, I looking for a way to yank 51,67 to my default register.

2 Answers2

3

If you want to copy the range specification you could add the following mapping:

vnoremap <silent><C-s> :call setreg('"', line("'<") .. ',' .. line("'>"))<cr>

In Visual mode is you hit Ctrl s the range specification will be copy in the " register.

e.g. if you select lines 51 to 67 and hit Ctrl s the " register will contain the string 51,67.

Alternatively you could do it using a range function:

function! CopyRange() range
    call setreg('"', a:firstline .. ',' .. a:lastline)
endfunction

vnoremap <silent><C-s> :call CopyRange()<cr>

Or if you want to stay in Visual mode taking advantage of the <cmd> qualifier you can do (Thanks to @ChristianBrabandt):

vnoremap <C-s> <cmd>call setreg('"', line("v") .. ',' .. line("."))<cr>

More information with :help line().

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
  • 1
    I'm a bit confused by the '[ and '] marks. According to :help '[ they are the first and last characters of inserted text. I would have expected '< and '> for visual selection instead. Sorry for the constant second-guessing. – Friedrich Feb 22 '24 at 21:11
  • 1
    @Friedrich, thanks for your second guessing :-) I was too fast. You were of course right with '< and '> but also the <cmd> makes that the marks were not set, only the second attempt was succesfull. I was too fast in my tests :-| This version is more carefully tested. Thanks again :-) – Vivian De Smedt Feb 22 '24 at 22:39
  • 2
    '< and '> are only updated once visual mode has ended (which is not the case for <cmd> and <scriptcmd>. In visual mode you can use line('v') and line('.') instead – Christian Brabandt Feb 23 '24 at 07:28
  • 2
    @ChristianBrabandt Thanks I didn't new the v position. – Vivian De Smedt Feb 23 '24 at 07:44
  • Sorry for the late comment and thank you so much for this code snippet! I had issue making setreg() work until I removed the following line to my vim configuration: set clipboard=unnamed. Do you have any idea why setreg() doesn't work with this configuration ? – Martin Delille Mar 03 '24 at 11:50
  • 1
    If you set clipboard to unnamed your default (the one to past and to copy) register is *. You can replace in the code setteg('"' by setreg('*' and keep your set clipboard=unnamed – Vivian De Smedt Mar 03 '24 at 11:59
  • Indeed thank you so much! Shall I edit my question to update this specific detail ? – Martin Delille Mar 05 '24 at 05:54
  • Thanks for the feedback. You could adapt your question and I'll update my answer then :-) – Vivian De Smedt Mar 05 '24 at 06:16
0

If you hit y it will goes into the default register ".

If you want it to go to the system clipboard (+ for Linux, * for Windows) you have to specify the register ("+y for Linux, "*y for Windows).

If you set the clipboard option to unnamedplus:

:set clipboard=unnamedplus

then by default the Vim will use the + register to yank (copy) and paste.

If you set the clipboard option to unnamed:

:set clipboard=unnamed

then by default the Vim will use the * register to yank (copy) and paste.

In such case you can just hit y to yank to the system clipboard.

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37