Learning the vi Editor/Advanced tasks

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Learning the vi Editor: Getting acquaintedBasic tasksMaking your work easierAdvanced tasksDetailsVi clones (VimBasic navigationModesTips and TricksUseful things for programmers to knowEnhancing VimVim on WindowsVimL Script language, Vile, BB vi)vi Reference

Copying and Pasting[edit | edit source]

Copying and pasting tasks are done with three keys, <y> (for "yank"), <d> (for "delete"), and <p> (for "paste"). In general, you type <y> or <d> 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 <v> to enter a visual mode. Then you can highlight text by moving cursor keys. And finally press <y> to copy text to clipboard. And <p> for paste.

A Word[edit | edit source]

To delete a single word, move your cursor to the first letter, then type <d><w>. To yank a single word, move your cursor to the first letter, then type <y><w>.

Other Methods[edit | edit source]

Move to the character past the last letter and type <d> <b>.

To delete a word like "can't", which has an apostrophe, move to the first character and type <d><W>. 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[edit | edit source]

To delete a single line, type <d><d>.

Other Amounts[edit | edit source]

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 <m><a>. 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>. (`a means "move to the character that has been marked as a")

Now move to some other position. Type <d><`><a>. 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 <a> used in this example. For instance, you might use <m><t> to mark the top of a section, or <m><b> 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 <m><m> to be a convenient temporary bookmark, because it can be typed so quickly.

To the end or beginning of a line[edit | edit source]

<d><$> or <d><^>

To the end or beginning of the file[edit | edit source]

<d><G> or <d><1><G>

To the next occurrence of a pattern[edit | edit source]

<d>/myPattern

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

Adjusting the Screen[edit | edit source]

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 <Ctrl-L> or <Ctrl-R>. Both commands do the same.

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

   [/pattern/][m]z[n](<CR>|.|-)

([ ... ] 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:

   <z><.>

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

   <z><CR>

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

   <z><->

If a /pattern/ or a number m is given the cursor is moved further after the adjustment. /pattern/ 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,

   /while/z<CR>

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.


Learning the vi Editor: Getting acquaintedBasic tasksMaking your work easierAdvanced tasksDetailsVi clones (VimBasic navigationModesTips and TricksUseful things for programmers to knowEnhancing VimVim on WindowsVimL Script language, Vile, BB vi)vi Reference