AssertionError
AssertionError

Reputation: 346

Vim how to copy to a real (no relative) line number

I'm working with NeoVim, but for the case I think it's the same if we talk about vim. I have set relative line numbers (set nu rnu) and I know how to copy from the line I currently have the cursor to line x with yxj, but I need to copy more lines than I can see, so I first go to line 247, then I go back to line 127 and I don't know if there's a way to specify that I want to copy to line number 247 (without subtracting, of course).

Regards

Upvotes: 1

Views: 586

Answers (2)

Enlico
Enlico

Reputation: 28406

  • Move to line 247, e.g. via 247gg
  • set a mark, e.g. a, via ma
  • move to line 127, e.g. via 127gg
  • yank from there to the mark y'a

All in all: 247ggma127ggy'a

Upvotes: 1

mattb
mattb

Reputation: 3063

The relevant documentation is :help change.txt. I'll highlight a few commands appearing in the :help copy-move sections of the change.txt help documentation (there is much more in there!).

You can use the command line to specify the lines to yank (i.e. copy, see :help :y):

:127,247y

It also works with relative numbers (and patterns - see :help range):

" yank lines 25 before cursor through 3 lines after
" cursor
:-25,+3y

In addition, if you know where you want to put them you can use the t command (see :help :t):

" copy lines 10 before cursor through 2 lines after
" cursor to 5 lines after the cursor
:-10,-2t+5

You can even mix and match relative and absolute lines (and patterns - see :help range):

" copy from line 23 through to 10 lines before cursor to
" line 51
:23,-10t51

For completeness there is the m command to move (i.e. cut and paste lines, see :help :t):

" move lines 12 before the cursor through to the current
" line to line 27
:-12,.m27

The traces.vim plugin

I find this plugin very nice - it highlights ranges for Ex commands as you type them on the command line (and shows you how the :substitute command will affect your file as you compose it). It really helped me start to use the command line more. I have this in my vimrc:

" fyi: there is extensive help documentation that's not on the github page 

"immediately highlight numerical ranges once you put the comma :N,N
let g:traces_num_range_preview = 1

" window used to show off-screen matches (just 5 since I only want the gist).
let g:traces_preview_window = "below 5new"

" if value is 1, view position will not be changed when highlighting ranges or
" patterns outside initial view position. I like this since I see it all in the
" preview window setting above
let g:traces_preserve_view_state = 1

Upvotes: 5

Related Questions