vi(m): copy multiple lines without doing math

kghose

New Member
Joined
Mar 26, 2024
Messages
20
Reaction score
12
Credits
163
I know Xyy will yank the next X lines. What I want to do is mark the current line, scroll down to somewhere else then copy (or cut) to that line. How can I do this? Thanks!
 


This is my usage for marking and copying to the mark which may be of interest for this text manipulation:

In command mode (Esc) mark the line with: ma
Navigate to the text to be copied and yank it.
Return the cursor to the marked line with: 'a
Paste the yanked text with: p

This may not satisfy a couple of your requirements as I understand them:
"copy ... to that line" and copying from the cursor position of the where the text is copied from.

The usage above will paste to the line below the marked line, and the command: 'a will return the cursor to the marked line. YMMV. Someone else may get closer for you.
 
I know Xyy will yank the next X lines. What I want to do is mark the current line, scroll down to somewhere else then copy (or cut) to that line. How can I do this? Thanks!
Place your cursor from where you want to copy and type y5y it will copy the 5 lines from the start of the cursor

1714542978510.png
 
Place your cursor from where you want to copy and type y5y it will copy the 5 lines from the start of the cursor

View attachment 19686
In standard vim, "type y5y" will copy 5 lines, not from the cursor, but the whole line. In order to copy from the cursor, one normally uses VISUAL mode to copy the exact text which is highlighted by cursor movement, and then yanked.
 
In standard vim, "type y5y" will copy 5 lines, not from the cursor, but the whole line. In order to copy from the cursor, one normally uses VISUAL mode to copy the exact text which is highlighted by cursor movement, and then yanked.
Yes if you use vim we have a feature called visual mode
 
I tend to use the "esc X yy" method in vi. But there is also something called gpm.
If you like rodents, you can use your mouse to copy and paste text in terminal consoles.

There is also a way to "cheat". If you're ssh'd to a linux system remotely from another computer
running Windows/MacOS, or Xwindows, you can usually use copy and text in the remote console.

Of course you can do that locally also, if you're running an Xwindows GUI there.

Also you can use head and tail, it can get a little convoluted but I've done something like this
in the last ( don't ask ). Lets say I have a text document 50 lines long and I want to copy lines 20 to 25.

head -n 25 mytext.file | tail -n 5 > anothertext.file ( that does require some math I guess )

Note: As I was writing this, I was wondering if many Linux systems no longer use X11.org as the Xwindows
back-end, do we still call it "X"windows?
 
Last edited:
This is my usage for marking and copying to the mark which may be of interest for this text manipulation:

In command mode (Esc) mark the line with: ma
Navigate to the text to be copied and yank it.
Return the cursor to the marked line with: 'a
Paste the yanked text with: p

This may not satisfy a couple of your requirements as I understand them:
"copy ... to that line" and copying from the cursor position of the where the text is copied from.

The usage above will paste to the line below the marked line, and the command: 'a will return the cursor to the marked line. YMMV. Someone else may get closer for you.
Thanks, I think this was the closest answer: Using a mark. I looked up some resources and the process is

1. Go to the first line to be copied
2. Mark it with ma
3. Go to the last line to be copied
4. Use y`m to yank from the mark to that line
 
Another option is to use one of vims visual modes. If you're copying entire lines of text, it's best to use "visual line" mode.

In command mode:
  • Go to the first line you want to copy
  • Press V - as in captial v / shift-v
  • Use vims navigation/motion keys to move the cursor to the last line you want to copy
  • Yank the text using y, or if you're planning to do a cut/paste use d to delete the text (which by default stores it in the copy/paste buffer)

Then move the cursor to the place you want to paste the text and use p to paste the copied block of text below the current line, or P to paste the copied block to the line above the cursor position.

If you're planning on pasting a block of text over the top of another block of text:
  • Navigate to the first line of the text you want to replace.
  • Enter visual line mode again and move the selection down to the last line you want to replace and then hit p to paste the copied text over the selected lines.

Another thing to consider:
If you plan to paste text from the copy/paste buffer over the top of a selection, the copied text will be copied over the top of the selected text but the selected text is deleted to the copy/paste buffer. So the copy/paste buffer ends up containing the lines of text you just replaced.

So if you plan to paste a copied piece of text multiple times in a document using a visual selection to select and replace other lines, you should yank the selected text to one of vims registers.

E.g.
Select the text you want to copy/cut using visual line mode, then yank using "my " specifies to use a register, m is the name of the register (you can use any letter from a to z for the register) and y to yank the selection to register m.

Or for a cut: "md, where "m specifies register m and d deletes the selected text to register m.

Then to paste, you can use:
"mp to paste from register m to the next line, or "mP to paste from register m to the previous line.

Or again, if using visual line mode to visually select a region of text to replace with the copied text, you select the lines to replace in visual line mode, then use "mp to paste from the register.

Because you’ve used a register, you can repeat the paste operation over visually selected areas as many times as you like.
Those lines of text will remain in register m until you overwrite the register with something else.

To sum up:
If you plan to paste to new lines of text - use visual selection with y or d to copy/cut to vims copy/paste buffer and then p or P to paste.

But if you plan to select an area to replace, consider copying/cutting to a named register, especially if you plan to use paste over more than one selected area. That way, you can recall the copied text from the register and all of the replaced text ends up in the default copy/paste buffer.

There is a way of recalling items from the default copy/paste buffers history. But I can’t remember how to do it without looking it up. I find It’s more convenient to just use a named register.

Another thing that can help you with making numbered actions in vim is to enable line numbers AND relative line numbers.
To do that, in command mode - enter the following command:
:set nu rnu

With line numbers AND relative line numbers shown, the current line is listed with whatever the current line number is. Lines above or below the current line are numbered from 1 onwards.
So the lines immediately above AND below the current line are numbered 1.
The next lines above AND below are numbered 2 etc.
E.g.:
2: some text
1: more text
102: <<<<This is the current line
1: blah blah blah
2: etc
3: waffle waffle
4: and so on….
That way you can just look at a line number and see how far away it is from the current line. So, we’re at line 102 and we want to delete from line 102 to the line that says "and so on….".

We enter visual line mode, we look at the line number next to the line containing "and so on…." and we see the number 4. So we know we want to move the selection four lines down. Instead of pressing j four times, we can simply type 4j to select the entire area and then delete using d.

So by setting the line numbering to relative numbers, we can instantly see how far away any line is from the current line. That way, when making selections or doing any other line-based repeatable motion, we can do so without having to use any mental arithmetic. That can save you from having to spam the j and k keys a bunch of times!

Edit:
One other option I forgot to mention is the register for the systems copy/paste buffer, or the "clipboard" as it’s often referred to.
Rather than yanking/copying/deleting to vims internal copy/paste buffer, or to a named register, you can also copy to the system clipboard using vims + register.
E.g.
"+y to yank to the system clipboard and "+p to paste from the system clipboard.

By copying to the system clipboard, you have the same benefit as using a named register - plus, you can also use the system clipboard to copy/paste to/from external applications.

So in Vim, you could yank a URL from a document into the system clipboard and then switch to your browser and hit ctrl v in the address bar to paste the URL.
 
Last edited:
Vim has 3 visual modes:

V: visual line mode (always use this for cutting/copying/pasting lines)

Ctrl+v: visual block mode, it's used to select blocks of text and make repeated edits to parts of several lines

v: visual character mode, used to make edits to characters individually. Unlike visual line mode, this copies and pastes forward in the text

Efficient vim use requires being proficient with all 3 of these modes, doing line math is slow and not fun.
 

Staff online

Members online


Top