block by roachhd 49e8de9e6cc596f5230f

get started with vim

Tutorial - Vim Tips Wiki

Quantcast

Wikia

‘ ); }

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!

Vim tutorEdit

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 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.

‘ + ‘

‘ ); } else if (window.fromsearch && typeof(Wikia.geo) !== ‘undefined’ && [‘GB’,’JP’,’UK’,’US’].indexOf(Wikia.geo.getCountryCode())

‘ + ‘

‘ ); }

Inserting textEdit

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, 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:

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 quittingEdit

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.

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 moreEdit

Find and tillEdit

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

: 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:

: You are now in insert mode. Type the replacement text, then press Esc.

: 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).

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)

** TO DO **

:

: I think that the proposed section about opening split windows is probably too much info for this tip, we should link elsewhere. Same goes for multiple buffers. Probably we need a “see also” section with sub-sections on editing multiple files, etc. The same might be applicable to the “other modes” info. Probably we need a new tip about all Vim’s various modes, if we don’t have one already. Or just a :help link if there is a suitable place to see info about all the different modes. –Fritzophrenic 14:35, August 19, 2010 (UTC)

Cancel Save