310

For X11-based systems (ie. Linux and most other UNIX-like systems) there are two clipboards which are independent of each other: PRIMARY - This is copy-on-select, and can be pasted with the middle mouse button. CLIPBOARD - This is copied with (usually) ^C, and pasted with ^V (It's like MS Windows). OS X and Windows systems only have one clipboard. For X11 ...


66

In Vim, use visual line mode: Put your cursor on the top line of the block of text/code to remove Press V (That's capital "V" : Shift + v ) Move your cursor down to the bottom of the block of text/code to remove Press d For deleting large blocks of text this is preferred over simple visual mode because you don't need to worry about which column the ...


59

From :help :yank: :[range]y[ank] [x] Yank `[range]` lines [into register x]. So, to yank line 4, one would type: :4yank Note you can easily do this from insert mode with <C-o>; this allows you to execute one command, after which you're returned to insert mode; for example: <C-o>:4yank You can, of course, also use other ranges. Some ...


49

First, you do not need to yank and delete; the latter will also put the deleted contents into the (default or specified) register. Therefore, ddp / ddkP are common commands to move a line one down / up. Alternatively, you can use the :move command, i.e. :move +1 / :move -2; this doesn't clobber the register, and you can also move entire ranges. You can ...


42

You can :set relativenumber, so you don't have to count. ;)


39

You can easy delete large block of text knowing its structure. If it's paragraph, use: d{ (e.g. you can prefix with number of paragraphs) If it's block of code within parenthesis, use: d% If the text appearing till end of the file, use: dG If you know how it ends, use: d/ and type ending text followed by Enter (you can also use regular expression, so the ...


39

Composing the yank operation with the so often used "one character to the right" motion should work: yl. BTW (to comment on a OP comment), for me » can be yanked in this manner, in gVim or terminal Vim. Perhaps a (file)encoding issue?


36

Since vim uses the percent sign to reference the current buffer, you can use it to get everything quickly. :%y will yank the entire buffer :%y+ will yank it to the + register (and presumably the clipboard, provided vim was compiled with the proper options). :%d and :%d+ will do the same for deletion. In each of these cases, the cursor remains in place.


35

Your options are: Use the black hole register _. For example, to delete a line you would type "_dd. This deletes without affecting the clipboard. Explicitly name a register for the original yank. For example, to yank a line into a named register you would type "ayy then to paste you would type "ap Use the yank registers to retrieve the original yank ...


34

The easiest is: :m+ or :m-2 which is abbreviation for :move as Ingo suggested. Or using visual mode (V) by cutting the line (d/x) then paste it (p - below cursor, P above cursor) after you moved your cursor to the right place before pasting (so in summary it's Vxp/Vdp). When moving multiple lines in visual mode, then you've to use :m '>+1 (to move one ...


34

In addition to Carpetsmoker's answer, I should point out the awesome :help :m and :help :t. If you want to copy line 4 to right below the current line you can do this: :4t. or that, if you want to copy that line right above the current line: :4t-


31

Assuming you've already yanked foo with ye or something similar, and that the cursor is somewhere on foo, you can use Wvep: W to go to bar (this is obviously optional and will depend on the current cursor position); v to start visual mode; e to go to the end of the word (you can also use other motions here, like iw); p to paste foo; this will replace the ...


29

"dap" does not actually delete everything. For example, try putting this into your buffer: 1 1 2 2 3 3 Then put your cursor on either '2' and type "dap". It'll leave the '1's and '3's. This is because "dap" deletes a paragraph. In fact, that's actually the mnemonic: "(D)elete (A) (P)aragraph". You can see it in the help under :h ap: ...


28

Operator-pending mode Between typing an operator (like d, c, or gU) and a motion (like w, i}, or /foo<CR>), Vim is in Operator-pending mode. You can create mappings for this using :omap and :onoremap. In my examples, I'm going to map the Operator-pending d to w. This is a random choice, because I don't know what you actually wanted to use it for. ...


22

You can use motions and/or text objects. For instance, you can delete everything from the cursor to the next blank line with d}. If the cursor is inside a paragraph, the paragraph may be deleted with dip or dap. If the cursor is within a HTML tag, you can delete it with dit or dat. Good mnemonics for these are "delete inside" and "delete around" (paragraph /...


21

The '[ and '] marks delimit the first and last line of the previously changed or yanked text. The `[ and `] counterparts delimit the respective lines & columns. Using that, you could visually select the last changed block of lines with '[V'] and then apply the = command. However, since a paste leaves your cursor at the first line of the content that ...


21

Use / for forward search: d/x<CR> and ? for backward search: d?x<CR>


19

You can do : wd4/x<Enter> If you start on the top left of your text Explanation w : move to beginning of next word d : delete 4/x<Enter> : until the 4th occurence of x If you don't know the number of times you would like to do it beforehand, you can also do : d/x<Enter> and then hit . to repeat


19

When you deleted the line using dd, you performed a linewise delete. The p command pastes after the cursor position. Since the default register was populated with linewise content, that means it will paste after the line the cursor is on. If you had instead deleted the contents of the line in a characterwise manner (e.g., 0D), then the register would have ...


19

You can use vi"p when inside "". This can replace text in yank register, so it matters when you want to use original yanked text more than once. EDIT: Additional info from Octaviour comment regarding making it reusable more than once: In order to do that I would yank to a named register, which makes the two commands: "ayi" and and vi""ap if you use ...


18

The other answers cover how to copy text from your buffer into the system clipboard. Another common operation is to copy text from another register to the clipboard. For example, if you've already yanked some text into " (the default register), you might want to load that register into the clipboard. You can do this with :let: let @+=@" — copies the ...


18

In fact, fg is "foreground" in Bash, but not in Vi(m). d3fg means: d - Delete until... 3 - Do the next motion 3 times f - Go to the next occurrence of the next character pressed. g - The character to go to. Thus, d3fg will delete up to (including) the third letter g on the line after the cursor. Thus the following text, with the cursor on the 0: ...


17

:% s/\v^(\d{4})(.*)$/\1\2 \1/ is one way to do it \v magic option, to avoid having to escape grouping () ^ start of line \d{4} match exactly four digits .* rest of line \1 \2 has the matched pattern within () edit: @Jair Lopez mentions in comments, the regular expression can be further improved: :% s/\v^(\d{4}).*/& \1/ or the equivalent :% s/\v^(\...


16

From http://vim.wikia.com/wiki/Accessing_the_system_clipboard set clipboard=unnamedplus This changes the default Vim register to the + register, which is linked to the system clipboard. From :h clipboard-unnamedplus: clipboard-unnamedplus unnamedplus A variant of the "unnamed" flag which uses the ...


16

I found a paper "An Introduction to Display Editing with Vi" by William Joy (vi creator) and Mark Horton (vi maintainer since 1979). From the paper it is clear that the default Y behavior is not a mistake, but a desired feature. In the "Rearranging and duplicating text" section they mention this: Try the command YP. This makes a copy of the current line ...


16

Yes. You want to use the "delete" operator instead of the "Change" operator. The delete operator is d<motion> and it deletes everything that <motion> moves over. In this case, the motion you want is $ which moves to the end of the current line. Of course, you may also use D which is simply a shortcut for d$


15

Use an uppercase register when yanking (copying): "Ay "A says to append to the a register, as opposed to "a which would replace the contents of the a register. Once you've copied everything into the register, you can then paste it all at once with: "ap


14

This mapping allows you to reselect the text you just pasted: nnoremap gV `[v`] But you should have used [p and ]p instead. See :help [p.


14

Usually I press vy (visual yank), sometimes xu (delete undo) too. Also , if you want to yank the char before your cursor, instead of lvy, you can press Xu, I think that's the reason why I press xu for the current. :-) Note that, Xu will make your cursor move to that char.


Only top voted, non community-wiki answers of a minimum length are eligible