Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy partial lines in VI

Tags:

vim

vi

There are a lot of different ways in which one can yank complete single/multiple lines. Is there a way in which we can copy partial lines in vi, like just 10 characters of the line.

like image 209
Aman Deep Gautam Avatar asked Oct 08 '12 10:10

Aman Deep Gautam


People also ask

How do I copy a section in vi?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.

How do I copy a chunk of text in Vim?

For copy: Place cursor on starting of block and press md and then goto end of block and press y'd. This will select the block to paste it press p. For cut: Place cursor on starting of block and press ma and then goto end of block and press d'a. This will select the block to paste it press p.


2 Answers

I would guess the most common partial yanks are:

yaw: yank the word the cursor is currently in
2yaw: yank the word the cursor in currently in and the next (2 words total)
ya(: yank the matched parentheses containing the cursor
yf.: yank from the cursor to the next .
y$: yank from the cursor to the end of the line

Any movement keys can be used.

like image 188
William Pursell Avatar answered Sep 19 '22 15:09

William Pursell


Cut and paste:

Position the cursor where you want to begin cutting.

  1. Press v to select characters (or uppercase V to select whole lines).
  2. Move the cursor to the end of what you want to cut.
  3. Press d to cut (or y to copy).
  4. Move to where you would like to paste.
  5. Press P to paste before the cursor, or p to paste after.
  6. Copy and paste is performed with the same steps except for step 4 where you would press y instead of d:

d = delete = cut

y = yank = copy

Resource: vim.wikia.com: Copy, cut and paste

like image 30
radistao Avatar answered Sep 19 '22 15:09

radistao