How in the name of all that is Holy, do I cut and paste in Vi?
Sunday, March 4th, 2007
That’s a rather long title to this post, but one that is deserved I feel. I must have been using vi (or vim or even elvis I suspect) for about 23 years now and I’ve never bothered working out how to cut and damn paste in it. Usually I just highlight the text I need (to store it in the local PCs clipboard), then delete it and then paste it back in with a right click. But it’s high time I learnt how to do it properly.
Quite embarrasingly I’ve been doing it half right all this time because whenever you delete lines in Vi, it stores them in a little buffer. All you need to do to stick the contents of this buffer into your document is to press “p” or “P” in command mode. “P” probably works how you’re expecting it to.
For example, type in “5dd” to delete the five lines starting from your cursor posisiton and then move to where you need the text and press “P” to put it there.
Copying is another bucked of cod. You need to “yank” the text you need using either a double lowercase y or a single uppercase one. As usual with Vi there’s a million modifers you can use, such as yw to copy just to the end of the current word. But I think I’ve typed enough on the subject now…