80/20 of vi

I’m going to take back what I said about being able to do 95% of what you need to do in vi. It was pointed out to me that cutting and pasting is well over 40% of what you need text editor for. What kind of text editor can’t cut and paste? So today I’m going to look at cutting and pasting in the vi text editor, and saying that now you’ll know 80% of vi, where the other 20% will take 80% of the time to learn.

Cutting and pasting in vi is really pretty easy to do, you just need to know the commands.

When you start off in vi, you’re in the command mode. Let’s mark some text for movement. One way of doing this is to put the cursor at the start of the text and then issue a ma command. This marks the current cursor location. Now move the cursor to the end of what you want copied ( in command mode, hjkl are left down up right if you need them) or pasted and use one of the following commands:

ma sets a mark at the start of the text to be copied
d’a cuts (deletes) the text from the current cursor to the mark location
y’a copies (yanks) from the current cursor to the mark location

Now move the cursor where you want to paste and use the p command. It’s as easy as that. You can also use these commands for copying and cutting:

yy copies a line
3yy copies 3 lines (change the number to what you want)
dd cuts (deletes) a line
3dd cuts (deletes) 3 lines (change the number to what you want)
p paste what you copied or cut wherever the cursor is

A web page that was extremely helpful to me was

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


All this time I've been using dd to copy lines....always needing to paste the original line back in place....silly....