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.

5
  • Does vi.stackexchange.com/q/3231/10604 answer your question?
    – D. Ben Knoble
    Feb 21 at 17:04
  • 3
    sorry my question was not clear: I don't want to yank the line content but the line number Feb 22 at 9:20
  • 1
    With the edit, this should stay open.
    – D. Ben Knoble
    Feb 22 at 17:31
  • What do you want to do with that register?
    – romainl
    Feb 23 at 15:17
  • 1
    Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons? It allow the question to rest :-) Feb 23 at 19:48

2 Answers 2

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().

8
  • 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 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 :-) Feb 22 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 Feb 23 at 7:28
  • 2
    @ChristianBrabandt Thanks I didn't new the v position. Feb 23 at 7:44
  • 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 Mar 3 at 11:59
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.

4
  • You might also include the range syntax :51,67yank
    – D. Ben Knoble
    Feb 21 at 17:02
  • 1
    "+ 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 at 22:17
  • 1
    I don't want to yank the line range content but the line range itself (51,67 in my example) Feb 22 at 9:22
  • 2
    Thanks for the feedback :-) I'm too much in Vim, when I read 51,67 my brain translate it into the content of the lines from 51 to 67 ;-) Feb 22 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.