Here are the commands used to switch modes.
i |
Enter insert mode and proceed to insert text before the current cursor position | Command mode |
a |
Enter insert mode and proceed to insert text after the current cursor position (a is short for "append") | Command mode |
I |
Position the cursor at the start of the line and then enter insert mode. | Command mode |
A |
Append text to current line (ie go to end of line and then enter "insert mode".) | Command mode |
v |
Enter visual mode | visual or insert mode |
<ESC> | Enter command mode | visual or insert mode |
R |
Enter replace mode | command mode |
start of line | 0 |
end of line | $ |
left | h |
right | l |
down | j |
up | k |
back one word | b |
forward one word | w |
back one paragraph | { |
forward one paragraph | } |
go to line 33 | 33G |
Use P to insert the text "before" the cursor, and
p
to put the text at the point after the cursor
To search for a string, while in command mode , hit
/
Followed by the search expression. Note that the search string is actually
a pattern with support for wildcards, etc. Try :help pattern
for details on how patterns work.
/foo | Search for the next instance of foo |
/foo.*bar | search for any line containing "foo" followed by "bar" (possibly with some stuff in between) |
/[0-9]\{3}[ -][0-9]\{4} | Search for a phone number (any 7 digit phone number) |
s
command. The format is this:
[address]s/search_pattern/replacement string/[falgs]
where the address can be line numbers, or a comma-seperated range of line numbers.
Patterns are involved little beasts, and a discussion of them here would take us too far afield. Use :help pattern
for more information on patterns. If this info looks ike Greek to you , then assuming that you aren't Greek,
read my Grep tutorial
which contains a step by step tutorial on how patterns work. Grep
patterns are slightly different from vim patterns in the syntax (ie the
symbols used are slightly different) , but conceptually, it's exactly
the same.
To put a delimiter (ie '/' ) in the search or replace string, you
'escape' it
with a backslash '\'. The backslash is used to escape all special
characters.
Some types of strings (like directory lists) use several backslashes, so in these cases, it makes sense to use a different delimiter. The delimiters usually used are :;/ but several other delimiters are allowed including these *%@ Some examples are given below. There are also special ways of denoting line numbers , for example:
. | current line |
.+3 | 3 lines below current line |
.-3 | 3 lines before the current line |
$ | The last line |
% | All lines from beginning to end |
g | Substitute all matches on specified line (the default only substitutes for the first match) |
i | use case insensitive matching |
c | prompt for confirmation prior to each substitution |
.s/foo/bar/g | Substitute all occurences of foo on the current line with bar |
%s/foo/bar/gi | Subsitute all matches of the word foo in the file with bar. Ignore case in searches. |
1,8s/<[^>]*>//g | Remove all html tags (ie strings enclosed in <> ) in lines 1 to 8. |
%s/foo/bar/gi | Subsitute all matches of the word foo in the file with bar. Ignore case in searches. |
.+3,$-1s:\(\(--\=\|+\)[a-zA-Z-]\):<EM>\1<EM>:g | Put an emphasis html tag around all unix command line options between three lines from the current line, and the second last line of the file |
%s;http://www.microsoft.com/;http://www.linux.org;g | Subsitute all matches of the URL http://www.microsoft.com with the URL http://www.linux.org. |
:r filename
:r! command
Example
:r! date
inserts a date stamp in the file
To filter a region through a command, use 'v' to highlight that region, then do the following : type
!command
and this will filter through command.
Example
!sort
will sort the selected text alphabetically.