How to Use Vimtutor?

How to Use Vimtutor?

Vim ships with its own tutorial. We highly recommended it! Follow the steps at :help tutor. If you're in a hurry, you can probably get by with some basic commands, but you should 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.

Launching Vim

To launch Vim, open a terminal, and type the command vim. You can also open a file by specifying a name: vim foo.txt. If foo.txt exists, it will be edited. Otherwise, it will be created.

Inserting text

By default, when Vim starts, you cannot simply type to enter text because Vim starts in normal mode (sometimes called command mode). While confusing for new users, normal mode provides the power of Vim because typing a few keys can perform many useful functions.

In normal mode, you can enter commands, for example, to copy, delete or indent text. You return to normal mode from other modes by pressing the Esc key.

You can enter insert mode from normal mode by pressing the i key. Now, the text you type will be inserted. You can enter visual mode from normal mode by pressing the v key. That starts a visual selection.

There are several more ways to enter insert mode, depending on where you want to insert the text:

  • i insert at a current location
  • an insert after current location (append)
  • I insert AT START of the current line
  • A insert AFTER END of the current line
  • o insert line below the current line (open)
  • O insert line ABOVE the current line
  • s delete the character under the cursor and start inserting in its place (substitute text)
  • S delete all text online and start inserting in its place (substitute line)
  • cw delete to the end of the current word and start inserting in its place (any movement command can be substituted for w)
  • cc same as S (change line)
  • C delete from the cursor to the end of the line and start inserting at the cursor position

For example, starting in normal mode, if you press A then type "hello" and press Esc, you will append "hello" to the end of the current line. If you move to another line and press. you will append "hello" to that line as well (. repeats the last operation). If you had used I (instead of A), the "hello"` would have been inserted at the start of the line and pressing. would repeat that operation.

Saving and quitting

Save the current file by entering :w (which always writes the file even if it has not changed), or :update (which only writes the file if it has changed). That is, press Esc to enter normal mode if necessary, then press the : key, then the w key, then press Enter. If Vim indicates a problem (for example, the file was flagged as read-only in Vim, or the file has been modified by another program since you began editing), you can use :w! to force Vim to write the file anyway.

If you are not editing an existing file (for example if you launched Vim with no arguments), you will need to provide a file name when you save. You can do this with :w filename or :saveas filename, for example, :w myfile.txt.

After saving your changes, you can quit Vim with :q. Or the saving and quitting can be combined into one operation with :wq or :x.

If you want to discard any changes, enter :q! to quit Vim without saving.

It is possible to have more than one file open in Vim, and there are commands for saving or quitting when working with multiple files (see :help window-exit):

:wa write all changed files (save all changes) :xa exit all (save all changes and close Vim) :qa quit all (close Vim, but not if there are unsaved changes) :qa! quit all (close Vim without saving—discard any changes)

The word "file" was used above, although the correct term is "buffer". A buffer is an area used by Vim to store a collection of text. Usually, a buffer has an associated file: to start, a buffer is filled by reading its file, and the buffer can be saved by writing to its file.

A file is read into a buffer when you start Vim with a file name argument (for example, vim myfile.txt), or when you issue the :e filename (edit) command within Vim.

However, it is easy to have buffers that are not associated with a file. Examples are starting Vim with no argument (which gives an empty buffer with no file name), or entering :new to create a new buffer within Vim.

Movement and more

  • Hands-on the home row asdf hjkl.
  • 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 backward.
  • gg moves to the first line, G moves to the 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.
  • ``: opens command line to enter "ex" commands :help
  • ! after an ex command ignores warnings from many commands, or changes the behavior subtly for others.
  • Objects and actions: at beginning of a word: d2w (action)(times)(object) (delete)(2)(words forward) this deletes including the trailing space; use de to delete to end of the word (leaving the trailing space).
  • d2b (delete)(2)(words backward).
  • hjkl are also objects! example: d3l (delete)(3)(right), 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). "Change" means delete the current text and enter insert mode in its place.
  • cb (change)(word backward), c3b (change)(3)(words backward).
  • Why c and d? Use cw to delete the word and enter insert mode (so you can type a new word finishing with Esc). Use dw to delete words, 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 the 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.
  • 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 PREVIOUS parens.

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 the result: (on one line).