Vim logo vim online Vim Book Ad

basic Tip #344: Cut / Copy / Delete / Paste Lines without knowing the number of lines

 tip karma   Rating 45/24, Viewed by 1293 

created:   October 15, 2002 11:57      complexity:   basic
author:   Kdr      as of Vim:   5.7

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 the command Mode, Go to the beginning of the first mark (lets say line 50).
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, <reg name from a-z>, <y-yank or d-delete>, 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

 rate this tip  Life Changing Helpful Unfulfilling 

<<Faster loading of large files | Visual Studio + vim Quickfix mode + cygwin + XFree86 >>

Additional Notes

[email protected], October 15, 2002 18:13
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.
[email protected], October 15, 2002 19:33
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"
[email protected], October 15, 2002 23:24
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.
Anonymous, October 17, 2002 6:02
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 22, 2002 3:17
:h guioptions
only 'a' needs to be added. my 'guioptions=gmrLtTa', clipboard is empty.
Anonymous, October 23, 2002 1:24
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.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo