Technology
 

Tutorial

From Vim Tips Wiki

(Redirected from Vim Tutor)

Tip 1581 Previous Next created February 16, 2008 · complexity basic · author Metacosm · version 7.0


To be rewarded by the power of Vim, you will need to learn to properly drive it. Following are some simple tutorial guides for getting started. Please ignore anyone who provides advice on how to configure Vim to operate like Notepad!

Contents

[edit] Vim tutor

Vim ships with its own tutorial. Highly recommended! Follow the steps at :help tutor. If you're in a hurry, you can probably get by with some basic commands, but you should definitely do the tutorial when you have about a half hour you can devote to it. Time spent will more than make up for itself with the productivity increase it will give you.

If you need more guided tutorials after completing the built-in one, there are Vim tutorials created by other users that you may want to try.

[edit] Overview

  • Hands on the home row asdf hjkl.
  • Three modes: normal mode (hit esc); insert mode (hit i); visual mode (hit v).
  • hjkl move in normal mode: h is left and moves left; l is right and moves right; j looks like a down arrow and moves down; k moves up.
  • w moves one word forward; 3w moves three words forward; b moves one word backward; 3b moves three words backwards.
  • gg moves to first line, G moves to last line, 123G moves to line number 123.
  • More moving: 8k moves eight lines up, 5j moves five lines down, 4l moves four characters right, 23h moves 23 characters left.
  • In normal mode :w to save, :q to quit, :w! to overwrite, :q! to quit without saving, :wq to save and quit, :wq! to overwrite and quit, :e file to open "file".
  • ! after a command ignores warnings.
  • : opens command line :help ex-cmd-index
  • Objects and actions: at beginning of word: d2w (action)(times)(object) (delete)(2)(words forward) this deletes including the trailing space; use de to delete to end of word (leaving the trailing space).
  • d2b (delete)(2)(words backward).
  • hjkl are also objects! example: d3l (delete)(3)(left), for hl we count individual characters, not words, for jk we count individual lines, d3k delete 4 lines up (3 plus current).
  • cw (change)(word), c3w (change)(3)(words).
  • cb (change)(word backward), c3b (change)(3)(words backwards).
  • Why c and d? Use cw to delete word and enter insert mode (so you can type a new word finishing with Esc). Use dw to delete word, staying in normal mode.
  • Use u to undo and Ctrl-r to redo, multiple times.
  • Autocompletion: "whatchamacallit" need to type it again? Type wh then press Ctrl-p to find the previous word that starts with "wh". Ctrl-p and Ctrl-n cycle through matches in previous and next order.
  • Visual mode: v3w (visual select)(3)(words); change selection with b and hjkl*.
  • After selecting: y will "yank" (copy); p will "put" (paste) at a new location (after the cursor; use P for before the cursor). Use y in visual mode and p in normal mode.
  • More ways to enter insert mode: i a o I A O
    • i insert at current location
    • a insert after current location (append)
    • o insert line below current line (open)
    • I insert AT START of current line
    • A insert AFTER END of current line
    • O insert line ABOVE current line
  • Convenience commands: dd delete current line; yy yank current line.
  • Searching: /regularexpression to search forward, ?regularexpression search backward; press n for next hit, or N for previous.
  • As before we can combine objects for more: y/) will yank everything to NEXT parens (or whatever you search for) while y?) will yank everything up to the LAST parens.

[edit] Find and till

It's rewarding to become familiar with the 'find' and 'till' commands.

  • Jump to a character in the same line: fx to find the next 'x' in the line, and Fx to find the previous one.
  • 'Till' is similar: tC to jump till just before the next 'C' in the line, and TC to jump till just after the previous one.
  • Use , and ; to jump to the previous and next occurrence of the character found with t, T, f, or F.
In the above, x is any character, including Tab (press f then Tab to jump to the next Tab on the current line).

Magic happens when you combine the motions find and till with operators:

  • ctx change all text till the next 'x' (x is any character; x is not changed).
  • cfx same, but include the 'x'.
You are now in insert mode. Type the replacement text, then press Esc.
  • dtx delete all text till the next 'x'.
  • dfx same, but include the 'x'.
  • Want to do it again? Save a keystroke with d; or save two with . (see repeat).
Example: You are at the start of a line:
This is an example (and here is more) and so on (on one line).
Type dt( to delete from the cursor till '(', with result:
(and here is more) and so on (on one line).
Type . to repeat, with result:
(on one line).

[edit] Comments

Even though the above does not allude to it, vim does support mouse clicks, arrow keys, and even menus — as a kind of afterthoughts, not part of Vim's "basic" command set. — Tonymec 07:28, 25 May 2009 (UTC)