Learning the vi Editor/Advanced tasks

Copying and Pasting
Copying and pasting tasks are done with three keys, (for "yank"),   (for "delete"), and  (for "paste"). In general, you type or  to tell vi that you're at the position where you want to start yanking or deleting some text. Then you need to tell vi where to stop, using cursor movement or other commands.

You can also simply use key to enter a visual mode. Then you can highlight text by moving cursor keys. And finally press to copy text to clipboard. And for paste.

A Word
To delete a single word, move your cursor to the first letter, then type. To yank a single word, move your cursor to the first letter, then type.

Other Methods
Move to the character past the last letter and type.

To delete a word like "can't", which has an apostrophe, move to the first character and type. Note the capital W. This tells vi to go all the way to the first whitespace character after the word.

Likewise, try dB.

A Line
To delete a single line, type.

Other Amounts
One of the great things about vi is that it lets you select a bunch of text without having to move your hand to your mouse.

Type. This will mark the current position that your cursor is at as mark a. You can go back to this position anytime you want from now on by typing. (`a means "move to the character that has been marked as a")

Now move to some other position. Type. This will delete everything from the current position to the position you marked as a.

Note that you can use nearly any key in place of the used in this example. For instance, you might use to mark the top of a section, or  to mark the bottom. These mnemonics are strictly in the user's head – vi doesn't care that t means top or b means bottom. Vi will remember all unique bookmarks, allowing the user to easily and quickly jump between them. Some users find using to be a convenient temporary bookmark, because it can be typed so quickly.

To the end or beginning of a line
or

To the end or beginning of the file
or

To the next occurrence of a pattern
/myPattern

This is particularly useful when editing HTML files with d/<

Adjusting the Screen
vi, as a visual screen-oriented editor has a number of useful commands to redraw or adjust the screen in case you find yourself somewhere where you don't want to be.

If you run in a Unix shell, it is possible that some background process writes to the same terminal. This will disturb vi's screen layout. In order to force vi to redraw the complete screen, press or. Both commands do the same.

If you want to adjust what is currently displayed, then the command is rather useful. It's a kind of Swiss army knife, and has a rather complex syntax:

[/pattern/][m] [n](|.|-)

([ ... ] denotes optional items, (...|...) denotes alternatives)

Before we explain the syntax in detail, here are some common applications of the command:

Scroll the screen so the current line becomes the middle line of the screen. The cursor remains on that line:

Scroll the screen so the current line becomes the top line on the screen:

Scroll the screen, so the current line becomes the bottom line of the screen

If a  or a number m is given the cursor is moved further after the adjustment. indicates to move the cursor to the first match of that pattern. m indicates to move the cursor to the mth line on the screen. So, for example,

would first scroll the screen so the current line becomes the top line on the screen, and then move the cursor to the first 'while' in the text from that position on.

The number n is a rather obscure parameter. If provided, it tells vi to behave as if the screen is just n lines high. The result is that only n number of lines are adjusted, and the rest of the screen is either ignored or cleared, presumably useful on slow terminals to avoid redrawing the screen unneccessarily.