NOTE: Read this entire lesson before executing any of the steps!!
1. Hold down the `<Ctrl>`{normal} key and press `g`{normal}. We call this
`<C-g>`{normal}. A message will appear at the bottom of the page with the
filename and the position in the file. Remember the line number for
Step 3.
NOTE: You may see the cursor position in the lower right corner of the
screen. This happens when the ['ruler']('ruler') option is set.
2. Press [G](G) to move you to the bottom of the file.
Type [gg](gg) to move you to the start of the file.
3. Type the number of the line you were on and then `G`{normal}. This will
return you to the line you were on when you first pressed `<C-g>`{normal}.
4. If you feel confident to do this, execute steps 1 through 3.
# Lesson 4.2: THE SEARCH COMMAND
** Type `/`{normal} followed by a phrase to search for the phrase. **
1. In Normal mode type the `/`{normal} character. Notice that it and the
cursor appear at the bottom of the screen as with the `:`{normal} command.
2. Now type 'errroor' `<Enter>`{normal}. This is the word you want to search
for.
3. To search for the same phrase again, simply type [n](n).
To search for the same phrase in the opposite direction, type [N](N).
4. To search for a phrase in the backward direction, use [?](?) instead
of `/`{normal}.
5. To go back to where you came from press `<C-o>`{normal} (keep `<Ctrl>`{normal} pressed down while pressing the letter `o`{normal}). Repeat to go back
further. `<C-i>`{normal} goes forward.
"errroor" is not the way to spell error; errroor is an error.
NOTE: When the search reaches the end of the file it will continue at the
start, unless the ['wrapscan']('wrapscan') option has been reset.
# Lesson 4.3: MATCHING PARENTHESES SEARCH
** Type `%`{normal} to find a matching ),], or }. **
1. Place the cursor on any (, [, or { in the line below marked ✓.
2. Now type the [%](%) character.
3. The cursor will move to the matching parenthesis or bracket.
4. Type `%`{normal} to move the cursor to the other matching bracket.
5. Move the cursor to another (,),[,],{ or } and see what `%`{normal} does.
This ( is a test line with ('s, ['s ] and {'s } in it. ))
NOTE: This is very useful in debugging a program with unmatched parentheses!
# Lesson 4.4: THE SUBSTITUTE COMMAND
** Type `:s/old/new/g` to substitute "new" for "old". **
1. Move the cursor to the line below marked ✗.
2. Type
~~~ cmd
:s/thee/the/
~~~
NOTE that the [:s](:s) command only changed the first occurrence of "thee" in the line.
3. Now type
~~~ cmd
:s/thee/the/g
~~~
Adding the g [flag](:s_flags) means to substitute globally in the line,
change all occurrences of "thee" in the line.
Usually thee best time to see thee flowers is in thee spring.
4. To change every occurrence of a character string between two lines, type
~~~ cmd
:#,#s/old/new/g
~~~
where #,# are the line numbers of the range of lines where the
substitution is to be done.
Type
~~~ cmd
:%s/old/new/g
~~~
to change every occurrence in the whole file.
Type
~~~ cmd
:%s/old/new/gc
~~~
to find every occurrence in the whole file, with a prompt whether to
substitute or not.
# Lesson 4 SUMMARY
1. `<C-g>`{normal} displays your location and the file status.
`G`{normal} moves to the end of the file.
number `G`{normal} moves to that line number.
`gg`{normal} moves to the first line.
2. Typing `/`{normal} followed by a phrase searches FORWARD for the phrase.
Typing `?`{normal} followed by a phrase searches BACKWARD for the phrase.
After a search type `n`{normal} to find the next occurrence in the same
direction or `N`{normal} to search in the opposite direction.
`<C-o>`{normal} takes you back to older positions, `<C-i>`{normal} to
newer positions.
3. Typing `%`{normal} while the cursor is on a (,),[,],{, or } goes to its
match.
4. To substitute new for the first old in a line type
~~~ cmd
:s/old/new
~~~
To substitute new for all 'old's on a line type
~~~ cmd
:s/old/new/g
~~~
To substitute phrases between two line #'s type
~~~ cmd
:#,#s/old/new/g
~~~
To substitute all occurrences in the file type
~~~ cmd
:%s/old/new/g
~~~
To ask for confirmation each time add 'c'
~~~ cmd
:%s/old/new/gc
~~~
# Lesson 5.1: HOW TO EXECUTE AN EXTERNAL COMMAND
** Type `:!`{vim} followed by an external command to execute that command. **
1. Type the familiar command `:`{normal} to set the cursor at the bottom of
the screen. This allows you to enter a command-line command.
2. Now type the [!](!cmd) (exclamation point) character. This allows you to
execute any external shell command.
3. As an example type "ls" following the "!" and then hit `<Enter>`{normal}.
This will show you a listing of your directory, just as if you were
at the shell prompt.
NOTE: It is possible to execute any external command this way, also with
arguments.
NOTE: All `:`{vim} commands must be finished by hitting `<Enter>`{normal}.
From here on we will not always mention it.
# Lesson 5.2: MORE ON WRITING FILES
** To save the changes made to the text, type `:w`{vim} FILENAME. **
1. Type `:!ls`{vim} to get a listing of your directory.
You already know you must hit `<Enter>`{normal} after this.
2. Choose a filename that does not exist yet, such as TEST.
3. Now type:
~~~ cmd
:w TEST
~~~
(where TEST is the filename you chose.)
4. This saves the whole file (the Vim Tutor) under the name TEST.
To verify this, type `:!ls`{vim} again to see your directory.
NOTE: If you were to exit Vim and start it again with `vim TEST`, the file
would be an exact copy of the tutor when you saved it.
5. Now remove the file by typing:
~~~ cmd
:!rm TEST
~~~
# Lesson 5.3: SELECTING TEXT TO WRITE
** To save part of the file, type `v`{normal} motion `:w FILENAME`{vim}. **
1. Move the cursor to this line.
2. Press [v](v) and move the cursor to the fifth item below. Notice that the
text is highlighted.
3. Press the `:`{normal} character. At the bottom of the screen
:'<,'>
will appear.
4. Type
`:w TEST`{vim}
where TEST is a filename that does not exist yet. Verify that you see
`:'<,'>w TEST`{vim}
before you press `<Enter>`{normal}.
5. Vim will write the selected lines to the file TEST. Use `:!ls`{vim} to see it. Do not remove it yet! We will use it in the next lesson.
NOTE: Pressing [v](v) starts [Visual selection](visual-mode). You can move
the cursor around to make the selection bigger or smaller. Then you can
use an operator to do something with the text. For example, `d`{normal}
deletes the text.
# Lesson 5.4: RETRIEVING AND MERGING FILES
** To insert the contents of a file, type `:r FILENAME`{vim}. **
1. Place the cursor just above this line.
NOTE: After executing Step 2 you will see text from Lesson 5.3. Then move
DOWN to see this lesson again.
2. Now retrieve your TEST file using the command
`:r TEST`{vim}
where TEST is the name of the file you used.
The file you retrieve is placed below the cursor line.
3. To verify that a file was retrieved, cursor back and notice that there
are now two copies of Lesson 5.3, the original and the file version.
NOTE: You can also read the output of an external command. For example,
`:r !ls`{vim}
reads the output of the `ls` command and puts it below the cursor.
# Lesson 5 SUMMARY
1. [:!command](:!cmd) executes an external command.
Some useful examples are:
`:!ls`{vim} - shows a directory listing
`:!rm FILENAME`{vim} - removes file FILENAME
2. [:w](:w) FILENAME writes the current Vim file to disk with
name FILENAME.
3. [v](v) motion :w FILENAME saves the Visually selected lines in file
FILENAME.
4. [:r](:r) FILENAME retrieves disk file FILENAME and puts it
below the cursor position.
5. [:r !dir](:r!) reads the output of the dir command and
puts it below the cursor position.
# Lesson 6.1: THE OPEN COMMAND
** Type `o`{normal} to open a line below the cursor and place you in Insert mode. **
1. Move the cursor to the line below marked ✓.
2. Type the lowercase letter `o`{normal} to [open](o) up a line BELOW the
cursor and place you in Insert mode.
3. Now type some text and press `<Esc>`{normal} to exit Insert mode.
After typing `o`{normal} the cursor is placed on the open line in Insert mode.
4. To open up a line ABOVE the cursor, simply type a [capital O](O), rather
than a lowercase `o`{normal}. Try this on the line below.
Open up a line above this by typing O while the cursor is on this line.
# Lesson 6.2: THE APPEND COMMAND
** Type `a`{normal} to insert text AFTER the cursor. **
1. Move the cursor to the start of the line below marked ✗.
2. Press `e`{normal} until the cursor is on the end of "li".
3. Type the lowercase letter `a`{normal} to [append](a) text AFTER the
cursor.
4. Complete the word like the line below it. Press `<Esc>`{normal} to exit
Insert mode.
5. Use `e`{normal} to move to the next incomplete word and repeat steps 3
and 4.
This li will allow you to pract appendi text to a line.
This line will allow you to practice appending text to a line.
NOTE: [a](a), [i](i) and [A](A) all go to the same Insert mode, the only
difference is where the characters are inserted.
# Lesson 6.3: ANOTHER WAY TO REPLACE
** Type a capital `R`{normal} to replace more than one character. **
1. Move the cursor to the first line below marked ✗. Move the cursor to
the beginning of the first "xxx".
2. Now press `R`{normal} ([capital R](R)) and type the number below it in the
second line, so that it replaces the "xxx".
3. Press `<Esc>`{normal} to leave [Replace mode](mode-replace). Notice that
the rest of the line remains unmodified.
4. Repeat the steps to replace the remaining "xxx".
Adding 123 to xxx gives you xxx.
Adding 123 to 456 gives you 579.
NOTE: Replace mode is like Insert mode, but every typed character deletes an
existing character.
# Lesson 6.4: COPY AND PASTE TEXT
** Use the `y`{normal} operator to copy text and `p`{normal} to paste it. **
1. Go to the line marked with ✓ below and place the cursor after "a)".
2. Start Visual mode with `v`{normal} and move the cursor to just before
"first".
3. Type `y`{normal} to [yank](yank) (copy) the highlighted text.
4. Move the cursor to the end of the next line: `j$`{normal}
5. Type `p`{normal} to [put](put) (paste) the text.
6. Press `a`{normal} and then type "second". Press `<Esc>`{normal} to leave
Insert mode.
7. Use Visual mode to select "item.", yank it with `y`{normal}, move to the
end of the next line with `j$`{normal} and put the text there with `p`{normal}
a) This is the first item.
b)
NOTE: you can use `y`{normal} as an operator: `yw`{normal} yanks one word.
# Lesson 6.5: SET OPTION
** Set an option so a search or substitute ignores case. **
1. Search for 'ignore' by entering: `/ignore`
Repeat several times by pressing `n`{normal}.
2. Set the 'ic' (Ignore case) option by entering:
~~~ cmd
:set ic
~~~
3. Now search for 'ignore' again by pressing `n`{normal}.
Notice that Ignore and IGNORE are now also found.
4. Set the 'hlsearch' and 'incsearch' options:
~~~ cmd
:set hls is
~~~
5. Now type the search command again and see what happens: /ignore <Enter>
6. To disable ignoring case enter:
~~~ cmd
:set noic
~~~
7. To toggle the value of a setting, prepend it with "inv":
~~~ cmd
:set invic
~~~
NOTE: To remove the highlighting of matches enter:
~~~ cmd
:nohlsearch
~~~
NOTE: If you want to ignore case for just one search command, use [\c](/\c)
in the phrase: /ignore\c <Enter>
# Lesson 6 SUMMARY
1. Type `o`{normal} to open a line BELOW the cursor and start Insert mode.
Type `O`{normal} to open a line ABOVE the cursor.
2. Type `a`{normal} to insert text AFTER the cursor.
Type `A`{normal} to insert text after the end of the line.
3. The `e`{normal} command moves to the end of a word.
4. The `y`{normal} operator copies text, `p`{normal} pastes it.
5. Typing a capital `R`{normal} enters Replace mode until `<Esc>`{normal} is
pressed.
6. Typing "[:set](:set) xxx" sets the option "xxx". Some options are:
'ic' 'ignorecase' ignore upper/lower case when searching
'is' 'incsearch' show partial matches for a search phrase
'hls' 'hlsearch' highlight all matching phrases
You can either use the long or the short option name.
7. Prepend "no" to switch an option off:
~~~ cmd
:set noic
~~~
8. Prepend "inv" to toggle an option:
~~~ cmd
:set invic
~~~
# Lesson 7.1: GETTING HELP
** Use the on-line help system. **
Vim has a comprehensive on-line help system. To get started, try one of
these three:
- press the `<HELP>`{normal} key (if you have one)
- press the `<F1>`{normal} key (if you have one)
- type
`:help`{vim}
Read the text in the help window to find out how the help works.
Type `<C-w><C-w>`{normal} to jump from one window to another.
Type `:q`{vim} to close the help window.
You can find help on just about any subject, by giving an argument to the
":help" command. Try these (don't forget pressing <Enter>):
~~~ cmd
:help w
:help c_CTRL-D
:help insert-index
:help user-manual
~~~
# Lesson 7.2: CREATE A STARTUP SCRIPT
** Enable Vim features. **
Vim has many more features than Vi, but most of them are disabled by
default. To start using more features you have to create a "vimrc" file.