sponsor Vim development Vim logo Vim Book Ad

basic Tip #386: Cut/copy and paste using visual selection

 tip karma   Rating 525/190, Viewed by 8673 

created:   December 9, 2002 18:49      complexity:   basic
author:   rainbrot      as of Vim:   5.7

Visual selection, although common in applications today, is a key feature that differentiates vim from traditional vi.

To cut (or copy) and paste using visual selection:
1. Position the cursor at the beginning of the text you want to cut/copy.
2. Press v to begin character-based visual selection (or upper case V to select whole lines, or Ctrl-V for a vertical block).
3. Move the cursor to the end of the text to be cut/copied. (While selecting text, you can perform searches and other advanced movement, a feature that sets vim apart from most other editors.)
4. Press d (as in "delete") to cut, or y (as in "yank", which I imagine meaning "yank so hard and fast that it leaves a copy behind") to copy.
5. Move the cursor to the desired paste location.
6. Press p to paste after the cursor, or P to paste before.

In gvim, visual marking (steps 1-3) can be replaced by selecting text using a mouse or similar pointing device, although I strongly prefer to navigate using the keyboard.

Bonus tip: To replace the selected text with new text (to be entered by you), press 'c' instead of 'd' or 'p' on step 4.  This deletes the selection and leaves you in insert mode.  Then, instead of (or prior to) steps 5-6, type your replacement text.

 rate this tip  Life Changing Helpful Unfulfilling 

<<some java & vim tips | A way to quickly prefix a char(or chars) to a parameter list, via :s >>

Additional Notes

Anonymous, December 9, 2002 23:15
If you just want to copy (yank) the visually marked text, you do not need to 'y'ank it. Marking it will already copy it.
Using a mouse, you can insert it at another position by clicking the middle mouse button.
This also works in across vim applications on Windows systems (clipboard is inserted)
:-))
niklas, December 10, 2002 12:01
This is a really useful thing in Vim. I feel lost without it in any other editor. I have some more points I'd like to add to this tip:

* While in (any of the three) Visual mode(s), pressing 'o' will move the cursor to the opposite end of the selection. In Visual Block mode, you can also press 'O', allowing you to position the cursor in any of the four corners.

* If you have some yanked text, pressing 'p' or 'P' while in Visual mode will replace the selected text with the already yanked text. (After this, the previously selected text will be yanked.)

* Press 'gv' in Normal mode to restore your previous selection.

* It's really worth it to check out the register functionality in Vim: ':help registers'.

* If you're still eager to use the mouse-juggling middle-mouse trick of common unix copy-n-paste, or are into bending space and time with i_CTRL-R<reg>, consider checking out ':set paste' and ':set pastetoggle'. (Or in the latter case, try with i_CTRL-R_CTRL-O..)


(Btw - of course I never actually feel lost: I never use any other editor. ;)
Anonymous, July 5, 2003 11:52
You can replace a set of text in a visual block very easily by selecting a block, press c and then make changes to the first line. Pressing <esc> twice replaces all the text of the original selection.

See *v_b_c*

[email protected], August 15, 2003 12:43
I would really like to know how I can replace one block of text with another block of text.  For example, I want to copy this (which I know how to do):

abc
123
xyx

into this:

11111111
22222222
3333333
44444444
55555555

so that it looks like this:

11111111
22abc222
3312333
44xyz444
55555555

Using the visual yank and put described above will not allow me to copy an arbitrary selection into a new visual region.  This would be really useful to me (especially right now)
[email protected], September 13, 2003 9:43
Visual marking is broken out-of-the-box with recent vims (>6.0). Maybe someone can post some details on how to repair it by adding some stuff to my .vimrc. I would use the docs but since I last downloaded vim so many other things have been broken that I'm still working on them and the docs are pretty hard to navigate. What has happened to my dearly beloved vim?
Anonymous, December 14, 2003 18:43
did you try ctrl + q for visual block?
Anonymous, March 18, 2004 9:44
In vim 6.2 (on windows) ctrl-q seems to be the work arround (but you have to use 'hjkl' instead of the arrow keys to block stuff in.  IMHO the defaut should be changed (as experts know what a .vimrc is and novices do not).
[email protected] - NOSPAM, March 27, 2004 21:57
On windows the <mswin.vim> script seems to be getting sourced for many users.
Result: more windows like behavior (ctrl-v is "insert copy here", instead of visual-block
selection).  Hunt down your system vimrc and remove sourcing thereof if you don't
like that behavior (or substitute <mrswin.vim> in its place, see vimscript#514 -- sourcing
<mrswin.vim> toggles between mswin/normal-vim behavior).

One can get some extra vimpower with <vis.vim> -- it allows one to perform ex
commands on visual-block selections (such as substitutes).  See vimtip#63.

With vimtip#588 one can sort lines or blocks based on visual-block selection.
Enjoy!

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.
Sponsored by Web Concept Group Inc. SourceForge Logo