Vim Tips Wiki
mNo edit summary
 
(proper explanation)
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
 
{{review}}
 
{{Tip
 
 
|id=344
 
|id=344
  +
|previous=343
|title=Cut / Copy / Delete / Paste Lines without knowing the number of lines
 
  +
|next=345
|created=October 15, 2002 11:57
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=Kdr
 
|author=Kdr
|version=5.7
+
|version=6.0
 
|rating=126/53
 
|rating=126/53
  +
|category1=
|text=
 
  +
|category2=
If you ever need to cut / copy / delete / paste lines without knowing the actual number of lines, here is what you should do.
 
 
}}
 
If you ever need to cut/copy/delete/paste lines without knowing the actual number of lines, here is what you should do.
 
#In normal mode, go to the beginning of the section that you want to yank.
  +
#Type <code>mk</code> to mark this spot as <code>k</code>.
  +
#Go to the end of the section you want to yank using whatever movement commands you like.
  +
#Type: <code>y'k</code> (<y-yank>, <single quote-go to mark>, k) To yank from the mark to the current location.
 
#You can paste those lines wherever you want with <code>p</code>
   
  +
Similarly, <code>d'k</code> will cut/delete the lines from the current location to the mark.
   
 
==Comments==
  +
{{todo}}
  +
*Mention the new relative line number feature of Vim 7.3. Then you can delete a number of lines with no counting anyway.
   
 
----
1. In the command Mode, Go to the beginning of the first mark (lets say line 50).
 
 
Be sure to check out visual mode. V will allow to select a range of lines, which you can then "ay, etc.
 
2. Type: mk
 
 
3. Go to the end of your selection (lets say 100), using j's or Ctrl -F or anything. You don't need to count the lines.
 
 
4. Type: "ay'k (i.e. Double Quotes, &lt;reg name from a-z&gt;, &lt;y-yank or d-delete&gt;, single quote, k
 
 
5. The above command copies those lines into register a.
 
 
6. If you do "ad'k , it will delete them from the current location and copies them into register a.
 
 
7. You can paste those lines wherever you want just as you print from registers, i.e. "ap
 
 
 
 
I use this a lot, since I don't need to count the number of lines.
 
 
 
 
-Kdr
 
}}
 
 
== Comments ==
 
Be sure to check out visual mode. V will allow to select a range of lines, which you can then "ay, etc.
 
 
This is one of the coolest features of vim.
 
This is one of the coolest features of vim.
   
tunacat--AT--yahoo.com
 
, October 15, 2002 18:13
 
 
----
 
----
A related thought:
+
A related thought:<br>
I like to think of " as into and ' as till.
+
I like to think of " as into and ' as till.<br>
 
So, something like "ay'k can be thought of as "into a yank till k"
 
So, something like "ay'k can be thought of as "into a yank till k"
   
Feral--AT--firetop.dontspamme.com
 
, October 15, 2002 19:33
 
 
----
 
----
I normally don't count the lines, but use a range using line numbers like ".,100y"
+
I normally don't count the lines, but use a range using line numbers like ".,100y"
('.' means line number at cursorpos). The range can be relative (".,+5"), of course you know the number of lines then.
+
('.' means line number at cursorpos). The range can be relative (".,+5"), of course you know the number of lines then.
So I don't have to move the cursor around just for yanking and put it again to the position whre I want to drop the lines.
+
So I don't have to move the cursor around just for yanking and put it again to the position whre I want to drop the lines.
   
Visual marking with the mouse is a powerful alternative, because you don' have to copy/delete complete lines.
+
Visual marking with the mouse is a powerful alternative, because you don' have to copy/delete complete lines.
You also can visually mark rectangle blocks of text by pressing &lt;CTRL-V&gt; in visual mode.
+
You also can visually mark rectangle blocks of text by pressing <CTRL-V> in visual mode.
   
Remember: VIM automatically copies the visually marked text, so you can simply drop it by clicking the middle mouse button
+
Remember: Vim automatically copies the visually marked text, so you can simply drop it by clicking the middle mouse button
 
at cursor position without using named registers.
 
at cursor position without using named registers.
   
ramming--AT--web.de
 
, October 15, 2002 23:24
 
 
----
 
----
 
Vim doesn't automatically copy the selected text unless 'clipboard' and 'guioptions' settings have specific values. (I believe they are autoselect and a, respectively.)
 
Vim doesn't automatically copy the selected text unless 'clipboard' and 'guioptions' settings have specific values. (I believe they are autoselect and a, respectively.)
   
Anonymous
 
, October 17, 2002 6:02
 
 
----
 
----
 
{{help|'guioptions'}} Only 'a' needs to be added. My guioptions is 'gmrLtTa', clipboard is empty.
:h guioptions
 
only 'a' needs to be added. my 'guioptions=gmrLtTa', clipboard is empty.
 
   
'''Anonymous'''
 
, October 22, 2002 3:17
 
 
----
 
----
Other good way is use / to find the place to which delete/copy, e.g., d/This sentence should be left&lt;CR&gt;. For smaller deletes, dt&lt;letter&gt; and df&lt;letter&gt; are also useful.
+
Other good way is use / to find the place to which delete/copy, e.g., d/This sentence should be left<CR>. For smaller deletes, dt<letter> and df<letter> are also useful.
   
'''Anonymous'''
 
, October 23, 2002 1:24
 
 
----
 
----
An easier-to-remember way:
+
An easier-to-remember way:<br>
Forget about registers. Go to desired start line, hit ma ("mark this line as 'a'"), then go to desired end line, and hit y'a or d'a ("yank or delete to the line marked 'a'"). Paste using p.
+
Forget about registers. Go to desired start line, hit ma ("mark this line as 'a'"), then go to desired end line, and hit y'a or d'a ("yank or delete to the line marked 'a'"). Paste using p.
   
  +
:I just updated the main how-to to reflect this simpler method.
A character-level way: (not line-level)
 
  +
 
A character-level way: (not line-level)<br>
 
Go to desired start character, hit ma (same as above), then go to desired end character (can be in a different line), and hit y`a or d`a (notice that it's the "tick", not the "apostrophe"). Paste using p.
 
Go to desired start character, hit ma (same as above), then go to desired end character (can be in a different line), and hit y`a or d`a (notice that it's the "tick", not the "apostrophe"). Paste using p.
   
sjspam at geeky point net
 
, December 21, 2002 22:27
 
----
 
 
If you need to select a series of consecutive lines, type V}
 
 
Lyle
 
 
lylez--AT--kc.rr.com
 
, January 21, 2003 21:31
 
----
 
I don't get it. Why would I use this incredibly convoluted method instead of just using visual mode?
 
 
 
'''Anonymous'''
 
, January 20, 2004 6:50
 
----
 
Less keystrokes of course :)
 
 
'''Anonymous'''
 
, February 23, 2004 11:19
 
 
----
 
----
Go to the start of block mark it as `mx` ( mark to buffer x ) then go to the end of block which you want to copy or delete and mark it as `my` ( mark to buffer y), do `x ( this moves to mark x i.e., start of block) and they do y`y ( copy the block) or d`y (delete the block)
+
Go to the start of the block and mark it as <code>mx</code> then go to the end of block and mark it as <code>my</code>.
   
  +
Do <code>`x</code> (move to start of block), then do <code>y`y</code> (copy the block) or <code>d`y</code> (delete the block).
I find it helpful and saves a lot of time
 
   
ls_manian At yahoo.com
 
, July 1, 2004 2:31
 
 
----
 
----
  +
Cool story bros, but emacs is way easier... C-<SPC> to set mark, move to end of region, C-w to kill region, C-y to yank region back.
<!-- parsed by vimtips.py in 0.330597 seconds-->
 
  +
:I know you're trolling, and I know this particular page needs work because the main tip overcomplicates a very simple issue, but I can't resist:
  +
:*Emacs: <C-Space>, Vim: ma
  +
:*Emacs, Vim: move to end of region
  +
:*Emacs: <C-w>, Vim: d'a (Emacs one keystroke ahead)
  +
:*Emacs: <C-y>, Vim: p (Vim catches up)
  +
:Result: tie.
  +
:Or you could use the "visual mode" comment:
  +
:*Emacs: <C-Space>, Vim: V (tie, because that's Shift+v)
  +
:*Emacs, Vim: move to end of region
  +
:*Emacs: <C-w>, Vim: d (Vim ahead by one)
  +
:*Emacs: <C-y>, Vim: p (Vim ahead by two)
  +
:Result: Vim Wins! Nice try!
  +
:I'm sure there are ways in both editors to make this even faster, and I don't want to get into it, but Emacs is certainly not "way easier" for this task.
  +
:--[[User:Fritzophrenic|Fritzophrenic]] 20:18, November 18, 2010 (UTC)

Revision as of 14:46, 21 August 2013

Tip 344 Printable Monobook Previous Next

created 2002 · complexity basic · author Kdr · version 6.0


If you ever need to cut/copy/delete/paste lines without knowing the actual number of lines, here is what you should do.

  1. In normal mode, go to the beginning of the section that you want to yank.
  2. Type mk to mark this spot as k.
  3. Go to the end of the section you want to yank using whatever movement commands you like.
  4. Type: y'k (<y-yank>, <single quote-go to mark>, k) To yank from the mark to the current location.
  5. You can paste those lines wherever you want with p

Similarly, d'k will cut/delete the lines from the current location to the mark.

Comments

 TO DO 

  • Mention the new relative line number feature of Vim 7.3. Then you can delete a number of lines with no counting anyway.

Be sure to check out visual mode. V will allow to select a range of lines, which you can then "ay, etc. This is one of the coolest features of vim.


A related thought:
I like to think of " as into and ' as till.
So, something like "ay'k can be thought of as "into a yank till k"


I normally don't count the lines, but use a range using line numbers like ".,100y" ('.' means line number at cursorpos). The range can be relative (".,+5"), of course you know the number of lines then. So I don't have to move the cursor around just for yanking and put it again to the position whre I want to drop the lines.

Visual marking with the mouse is a powerful alternative, because you don' have to copy/delete complete lines. You also can visually mark rectangle blocks of text by pressing <CTRL-V> in visual mode.

Remember: Vim automatically copies the visually marked text, so you can simply drop it by clicking the middle mouse button at cursor position without using named registers.


Vim doesn't automatically copy the selected text unless 'clipboard' and 'guioptions' settings have specific values. (I believe they are autoselect and a, respectively.)


:help 'guioptions' Only 'a' needs to be added. My guioptions is 'gmrLtTa', clipboard is empty.


Other good way is use / to find the place to which delete/copy, e.g., d/This sentence should be left<CR>. For smaller deletes, dt<letter> and df<letter> are also useful.


An easier-to-remember way:
Forget about registers. Go to desired start line, hit ma ("mark this line as 'a'"), then go to desired end line, and hit y'a or d'a ("yank or delete to the line marked 'a'"). Paste using p.

I just updated the main how-to to reflect this simpler method.

A character-level way: (not line-level)
Go to desired start character, hit ma (same as above), then go to desired end character (can be in a different line), and hit y`a or d`a (notice that it's the "tick", not the "apostrophe"). Paste using p.


Go to the start of the block and mark it as mx then go to the end of block and mark it as my.

Do `x (move to start of block), then do y`y (copy the block) or d`y (delete the block).


Cool story bros, but emacs is way easier... C-<SPC> to set mark, move to end of region, C-w to kill region, C-y to yank region back.

I know you're trolling, and I know this particular page needs work because the main tip overcomplicates a very simple issue, but I can't resist:
  • Emacs: <C-Space>, Vim: ma
  • Emacs, Vim: move to end of region
  • Emacs: <C-w>, Vim: d'a (Emacs one keystroke ahead)
  • Emacs: <C-y>, Vim: p (Vim catches up)
Result: tie.
Or you could use the "visual mode" comment:
  • Emacs: <C-Space>, Vim: V (tie, because that's Shift+v)
  • Emacs, Vim: move to end of region
  • Emacs: <C-w>, Vim: d (Vim ahead by one)
  • Emacs: <C-y>, Vim: p (Vim ahead by two)
Result: Vim Wins! Nice try!
I'm sure there are ways in both editors to make this even faster, and I don't want to get into it, but Emacs is certainly not "way easier" for this task.
--Fritzophrenic 20:18, November 18, 2010 (UTC)