24

I find that the procedure to copy and paste in VIM is a bit tricky:

  • Visual mode
  • Y
  • p (or shift p)
  • insert mode

and if you wrongly hit the "delete" key, you lost the content (it is replaced by the char you deleted) and you have to re-copy it!

I've been working with Vim for several months, and I enjoy it, but the copy and paste mechanism annoys me.

Is there a way to simplify it? or some simpler alternative?

thank you

Alessandro De Simone
  • 4,085
  • 6
  • 28
  • 41

8 Answers8

20

Vim maintains a list of the last 9 deletes you have made in the numbered registers. So "0p will paste the most recent thing you have yanked, "1p will paste the 2nd most recent thing you have deleted or changed (note this does not include text that was simply yanked), and so on for registers 2-9.

If you want to use more than the last 9 deleted items or if you want an easy way to access previous yanks I would recommend the yankring plugin which, among other things, lets you see your last 100 yanks and choose which one you want to paste with the :YRShow command.

Finally if you want to paste without leaving insert mode you can use Ctrl-R to insert the contents of any register. So in insert mode typing <Ctrl-R>" would insert the contents of the unnamed register " (which is where yanked text gets put when no register is specified).

David Brown
  • 13,336
  • 4
  • 38
  • 55
  • 3
    Don't you mean `"0p`, `"1p`, etc.? – Herbert Sitz Jan 22 '12 at 04:58
  • 3
    +1 I have used gVim for several years but neither numbered registers nor Ctrl-R were familiar to me. – johnny Jan 22 '12 at 09:05
  • I new about `Ctrl-o`, but not 'Ctrl-R`. Thanks for that. +1 – Jason Down Jan 22 '12 at 13:16
  • 1
    `"0p` will paste the most recent thing you have *yanked*, not *yanked and deleted*. `"1p` will do this for *deleted*, *only* for deleted, same applies to `"2p` (second most recent deleted thing) and so on. You can find this in `:h quote_number`. – ZyX Jan 22 '12 at 16:06
  • I never knew copy and paste could be such an extensive precise tool. – eusid Sep 10 '13 at 05:54
11

Though not exactly "simplified", you do have the option to copy your text into a different register than where deleted text goes by default. This would protect you from losing what you copied by accidentally deleting something.

To copy to register a (as an example) you would do the following:

" a y [movement command]

Then to paste your text you would do:

" a p

One more thing you can do as well. If you want to paste while in insert mode, you can press ctrl+o and then enter your paste command (such as the register paste command I metioned above). The ctrl+o command is equivalent to pressing esc, but for one single command. Read more about it here.

Lastly, you always have the choice to write a vim script to change default behaviour to your liking (or perhaps find a plugin such as yankring).


UPDATE

Based on a conversation in the comments with kellogs, I decided to look into how to use any register for copy/paste, but in visual mode for the copy command (easier to visually see what you are grabbing). It turns out this is possible.

To copy to register a, but in visual mode:

v (moves into visual mode)

Then you do the same as above, but do the movement command first:

[movement command] "ay (highlight your selection via the movement command, then yank into register a).

Pasting is done the normal way as listed above the UPDATE.

Community
  • 1
  • 1
Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • Not sure why it doesn't work for you. I just opened up a random text file, moved to an arbitrary line and entered the following: `" a y 2 w` which pulled 2 words from my current cursor position and stored them in register a. I was then able to paste via `" a p` (tried in several locations, including the same file, a new tab, a different existing file). – Jason Down Oct 22 '13 at 19:21
  • i might want to retry this. Is there any visual confirmation ? – kellogs Oct 22 '13 at 22:47
  • Not really. In my example, it is the same as if you had just typed `y 2 w` (yank 2 words) and used the default register instead of register `a`. Your cursor changes shape (in my version of GVIM) and also shows all the keypresses down in the status line. Once you press the last key press (`w` in this case), it changes the cursor back to normal. – Jason Down Oct 23 '13 at 09:12
  • Works indeed. But pretty cumbersome without visual mode :/ – kellogs Oct 23 '13 at 11:07
  • Found a way to do it in visual mode and added an update. See update (basically go into visual mode, then instead of `" a y 2 w`, you would do `2 w " a y`, where the movement command comes first instead of last). – Jason Down Oct 23 '13 at 17:03
  • awesome. Even better - just use arrows in visual mode then " a y and " a p – kellogs Oct 23 '13 at 17:29
  • Ya for sure. How every you prefer to do movement commands. At least you'll see what you're doing in visual mode. – Jason Down Oct 23 '13 at 18:51
8

Vim does not completely throw away yanked text when you delete something, it is just overriding " register. Last yanked text is always stored in the register 0, thus you can replace p with "0p in your workflow or, better, map this to ,p and use it:

nnoremap ,p "0p
xnoremap ,p "0p

Another way is to throw away deleted text (delete it to black hole register _): "_dd, "_x and so on. I personally use ,d mapping for this:

nnoremap ,d "_d
xnoremap ,d "_d

In this case Vim won’t override any registers.

Third is using letter registers, as @Jason Down suggested.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • Nice idea. Also for completeness: nnoremap ,D "_D xnoremap ,D "_D nnoremap ,x "_x xnoremap ,x "_x – Magnus Sep 20 '12 at 21:49
2

For me the simplest solution was to create a key combo mapping that modally toggles this deletion side effect off or on.

With my script in place, I type ,, and I'm in "no side effect" mode, so deleting text has no effect on my yank buffer, so I can yank-delete-paste freely and efficiently all I like.

Then when done I type ,, again and vim goes back to its default behavior with delete-key side effects.

I find this solution to be ideal because I don't need to think about buffers every time I want to copy paste. See here for the script:

vim toggling buffer overwrite behavior when deleting

Community
  • 1
  • 1
Magnus
  • 10,736
  • 5
  • 44
  • 57
2

When I don't wish to change modes, I simply use the system clipboard to copy and paste.

Or I use CTRL-Y, If I'm just copying characters from the previous line.

If you want to copy the content an entire line from elsewhere in the buffer, type the first few characters and use CTRL-X CTRL-L.

For more information, consult:

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
1

Vi (and Vim) works very differently compared to a normal text editor such as Gedit. It also has a pretty steep learning curve. If you want to learn some basic commands, start with this interactive tutorial.

However, to answer you question. The system clipboard's content can be accessed through the plus register. So to paste something from the system clipboard you can, from the Normal mode, press: "+p (Not at the same time, but one after another).

1

Nothing much to simplify - that's how Vim works. Well, more or less.

However, regarding your problem of deleting (therefore, replacing the contents of the register) - you can use YankRing for example. It provides some "user-friendliness" to Vim's yank/paste operations.


How it works?

You go to the first line, and yank it ("copy" in Windows terms). Then you go to the second line and yank that too. Then you to the seventh line and yank that one as well.

Then you go ... anywhere, and paste(!). You get the last (seventh) line. Press a shortcut key, and that last paste is replaced with the previous yank (the second line). Again ... the first line :)

So, in that way, you don't really have to worry about losing your yanked contents, which from what I undestood was your main problem.

Rook
  • 60,248
  • 49
  • 165
  • 242
0

The thing, here is that you are not supposed to "return to insert mode".

Editing (yanking, deleting, replacing, etc.) is done in normal mode, insert mode is only for typing text.

If you are in insert mode and want to reuse some text already present elsewhere in your document you can hit:

  • <C-n> and <C-p> to insert a word that starts with the characters on the left of the cursor
  • <C-x><C-l> to insert a line that starts with the characters on the left of the cursor
romainl
  • 186,200
  • 21
  • 280
  • 313