简体   繁体   中英

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

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 (ie 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 place them you can use the t command (see :help :t ):

" copy lines 10 before cursor through 2 lines after
" cursor to after 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 (ie 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
  • Move to line 247,
  • set a mark, eg a , via ma
  • move to line 127
  • yank from there to the mark y'a

All in all: 2 4 7 g g m a 1 2 7 g g y ' a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM