To become an expert in VIm takes lots of pratice. This article is a place for me to remember the commands and how they are used. I hope you find it useful.

Saving changes to a file

Shift + : write

or

Shift + : w

or

Z Z

Quiting vim

Shift + : quit

Shift + : q

Marking

m then x

Marks the current position with x (x can be any letter). (The original vi allows only lowercase letters. Vim distinguishes between uppercase and lowercase letters.)

``

Returns to the exact position of the previous mark or context after a move.

/ pattern

Look forward for the pattern.

Replace

:50,100s/ old/ new/ g

Replace all occurances of old with new between lines 50 and 100.

:1,30s/ his/ the/ gc

Confirm all replacements of his with the between lines 1 and 30.

  1. Use n to goto the next match.
  2. Use . to repeat a change.
:g/pattern/s/old/new/g

The first g tells the command to operate on all lines of a file. pattern identifies the lines on which a substitution is to take place. On those lines containing pattern, ex is to substitute (s) for old the characters in new. The last g indicates that the substitution is to occur globally on that line.

Pattern matching rules

. 

(period, dot) Matches any single character except a newline. Remember that spaces are treated as characters. For example, p.p matches character strings such as pep, pip, and pcp.

*

Matches zero or more (as many as there are) of the single character that immediately precedes it. For example, bugs* will match bugs (one s) or bug (no s). (It will also match bugss, bugsss, and so on.) The * can follow a metacharacter. For example, since . (dot) means any character, .* means “match any number of any character.” Here’s a specific example of this: the command :s/ End.*/ End/ removes all characters after End (it replaces the remainder of the line with nothing).

^

When used at the start of a regular expression, requires that the following regular expression be found at the beginning of the line. For example, ^ Part matches Part when it occurs at the beginning of a line, and ^… matches the first three characters of a line. When not at the beginning of a regular expression, ^ stands for itself.

$

When used at the end of a regular expression, requires that the preceding regular expression be found at the end of the line; for example, here: $ matches only when here: occurs at the end of a line. When not at the end of a regular expression, $ stands for itself.

\

Treats the following special character as an ordinary character. For example, \\. matches an actual period instead of “any single character,” and \\* matches an actual asterisk instead of “any number of a character.” The \\ (backslash) prevents the interpretation of a special character. This prevention is called “escaping the character.” (Use \\\\ to get a literal backslash.)

External

:r !date

You can combine :read with a call to Unix, to read the results of a Unix command into your file.

comments powered by Disqus