Vi

Jump to: navigation, search

The vi editor is important, if for no other reason that you are likely to find vi on any Linux or posix system. Once you learn it you should be able to edit files on most systems. For one Trilug members comments on learning vi see Rick D learns vi.

Contents

[edit] vi clones and extensions

There have been several implementations of vi, although they provide extensions which you might not find everywhere. To get the full value of learning vi, you need to first learn vi and not the extensions.

Depending on the distribution or user settings, the vi command may actually invoke any of these clones.

[edit] Vim

Vim is a clone of vi which is popular in the Linux world, mainly because of its more intuitive usage when in edit mode, and its extended features. Most distributions provide a fully configured vim with syntax highlighting for most text file types, useful default settings, and intuitive key bindings.

Something useful to remember for Vim is the command :help for getting help.

[edit] Others

[edit] nvi

[edit] elvis

[edit] vile

[edit] Navigation

When opening a file you will initially be in command mode. For vi you need to be in command mode to navigate through the file. Vim is often configured to allow you to use the arrow keys for basic navigation when in edit mode, in addition to the standard navigation mechanisms.

[edit] Basic keys

These can be confusing at first, but with a little practice they'll come naturally. You may not want to use them all the time, but at least remember what they are ... you never know when you'll run across a machine that doesn't have the arrow keys bound, or has an older version of vi.

A useful feature of vi is that you can repeat any command (those that you don't enter at the prompt) mutliple times by typing the number of times you want to repeat it, before typing the command. In terms of navigation, typing 5<down> will take you 5 lines down, for example.

Often the page down and up keys will also be bound for you.

[edit] Other commands

[edit] Searching

In command mode, type a forward slash (/), which will then take you to a prompt at the bottom of the screen where you can type the text you want to search for. Hit enter to start the search. Search results will be hilighted, and the cursor will be placed at the first search result. To get to the next result, type n; or N to get to the previous search result.

To search backwards, type a question mark (?) in command mode, and then enter your search expression at the prompt. When searching backwards, the n and N commands are reversed, because you're working backwards.

If you're tired of seeing everything hilighted, type the command :nohls (no hilight search).

[edit] Search and replace

To replace all instances in a document, in command mode type:

   :%s/<search term>/<replace with>/<options>

Options:

For example:

   :%s/this/that/

To search and replace only in a range of lines, in command mode type:

   :<start line>,<end line>s/<search term>/<replace with>/<options>

For example:

   :5,33s/this/that/gc

[edit] Command Basics

Note: for commands which specify an optional number (num), leaving the number out implies doing the action once.

   i <begin typing>
   I <begin typing>
   a <begin typing>
   A <begin typing>
   [num]x
   [num]X
   Esc
   R <begin typing>
   [num]r <type character>
   c[num]w <begin typing>
   
   or
   
   [num]cw <begin typing>
   c$ <begin typing>
   c0 <begin typing>
   c^ <begin typing>
   d[num]w
   
   or
   
   [num]dw
   d$
   d0
   d^
   <num>dd
   <num>yy
   p (lower case)
   P (upper case)

Note that any of the deletes mentioned above populate the default register, and are available for pasting.

   . (dot)
   :w [filename]
   
   The filename is only necessary if you want to save to a different file.
   :q
   :wq
   :q!
   :e <filename>
   :r <filename>
   :r !<command>

[edit] Useful links

Retrieved from "http://www.trilug.org/wiki/Vi"