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.
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.
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().
'[ 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
'< 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
'< 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
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
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
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.
+ for Linux, `for Windows"* - yes, but only half the truth. See:help quote+`. Btw, there's a really nice and short article on X selections by jwz. You only have to stop reading when he goes into Emacs specifics.
– Friedrich
Feb 21 '24 at 22:17
51,67 in my example)
– Martin Delille
Feb 22 '24 at 09:22
51,67 my brain translate it into the content of the lines from 51 to 67 ;-)
– Vivian De Smedt
Feb 22 '24 at 10:09