How To Use Vim Tutorial

Vim is a powerful editor that has many commands, so many that it would be impossible to teach in a tutorial like this. This tutorial is designed to display enough commands so that you are comfortable to easily use Vim as a generic text editor.

MOVING THE CURSOR

** To move the cursor, press the h, j, k, l keys as shown. **

Hint: The h key is on the left and moves left
Hint: The l key is on the right and moves to the right
Hint: The j key looks like the down arrow
Hint: and of-course, the k key moves up

1. Now you can practice and move the cursor around the screen until you feel comfortable.

2. Using the j key we can now move to the next lesson.

Note: If you are unsure about what you typed, press the [ESC] key to place it in Normal Mode. Then retype the command you wanted.

ENTERING AND LEAVING VIM

Note: Before performing any of the steps below, read the entire lesson !!

1. Press the [ESC] key (to make sure you are in Normal Mode).

2. Type -> :: q! [RETURN]. This exits the editor without saving any changes you have made. If you want to save the changes and exit, type -> : wq [RETURN]

3. When you see the shell prompt (%) enter: vim tutor.pt_BR [RETURN]. Or enter the command that puts you in this tutor.

—> ‘vim’ is the command that enters the editor, ‘tutor.pt_BR’ is the file you want to edit.

EDITING TEXTS – DELETION

** In Normal Mode press x to delete the character under the cursor. **

1. Move the cursor to the line below marked with >.

2. To correct errors, move the cursor until it is over the character to be deleted.

3. Press the x key to delete the unwanted character.

4. Repeat steps 2 through 4 until the sentence is correct.

EDITING TEXTS – INSERT

** While in Normal Mode, press i to enter the text. **

1. Move the cursor to the first line below marked with >.

2. To make the first line equal to the second, move the cursor over the first character AFTER where the text will be inserted.

3. Press i and enter the required additions.

4. As soon as each error is corrected press [ESC] to return to Normal Mode. Repeat steps 2 through 4 to correct the sentence.

DELETE COMMANDS

** Type dw to delete to the end of a word. **

1. Press [ESC] to make sure you are in Normal Mode.

2. Move the cursor to the line below marked with >.

3. Move the cursor to the beginning of the word that needs to be deleted.

4. Type dw to make the word disappear.

Note: The letters dw will appear on the last line of the screen as soon as you type them. If you have entered something wrong, press [ESC] and start again.

MORE COMMANDS OF DELETION

** Type d $ to delete to the end of the line. **

1. Press [ESC] to make sure you are in Normal Mode.

2. Move the cursor to the line below marked with >.

3. Move the cursor to the end of the correct line (AFTER the first.).

4. Type d $ to delete to the end of the line.

COMMANDS AND OBJECTS

The format of the d delete command is as follows:

[Number] d object OR d [number] object

At where:

Number – indicates how many times the command will be executed (optional, default = 1)

D – is the delete command.

Object – is how the command will operate (listed below).

A short list of objects:

W – from the cursor to the end of the word, including space.

And – from the cursor to the end of the word, NOT including the space.

$ – from the cursor to the end of the line.

AN EXCEPTION TO ‘COMMAND-OBJECT’

** Type dd to delete a whole line. **

Because of the frequency of deleting a whole line, Vim designers decided that it would be simpler to type two d’s in a line to delete it.

1. Move the cursor to the second line in the list below.

2. Enter dd to delete the line.

3. Now move to the fourth line.

4. Type 2dd (remember the number-command-object) to delete the two lines.

COMMAND UNDO (UNDO)

** Press u to undo the last command, U restores an entire line. **

1. Move the cursor to the line marked with —> and position it on the first error.

2. Enter x to delete the first unwanted character.

3. Now enter u to undo the last executed command.

4. Correct all errors on the line this time using the x command .

5. Now, enter a capital U to return the line to its original state.

6. Now enter u a few times to undo the U and the previous commands.

7. Now type CTRL-R (holding the CTRL key while typing R) a few times to redo the commands (undo the undo’s).

THE PUT COMMAND

** Type p to place the last deletion after the cursor. **

1. Move the cursor to the first line in the list below.

2. Type dd to delete the line and save it to the Vim buffer.

3. Move the cursor to the line ABOVE where the deleted line should be.

4. In Normal Mode, type p to insert the line.

5. Repeat steps 2 through 4 to place all rows in the correct order.

COMMAND REPLACE (REPLACE)

** Type a character to replace a character under the cursor. **

1. Move the cursor to the first line below marked with >.

2. Move the cursor until it is on top of the first error.

3. Type then the character that should correct the error.

4. Repeat steps 2 through 3 until the first line is correct.

THE CHANGE COMMAND (CHANGE)

** To change a whole word or part of it, type cw. **

1. Move the cursor to the first line below marked with >.

2. Position the cursor on or on lunba.

3. Type cw and the correct word

4. Press [ESC] and move to the next error (the first character to be changed).

5. Repeat steps 3 through 4 until the first sentence equals second.

MORE CHANGES USING c

** The change command is used with the same objects as delete. **

1. The change command works in the same way as delete. The format is: [number] c object OR c [number] object

2. Objects are also the same as w (word), $ (end of line), etc.

3. Move to the first line below marked with >.

4. Move the cursor to the first error.

5. Type c $ to make the rest of the line equal to the second and press [ESC].

FILE LOCATION AND STATUS

** Type CTRL-g to show your location in the file and its status. Type SHIFT-G to move to a line in the file. **

Note: Read this entire lesson before performing any of the steps !!

1. Hold down the Ctrl key and press g. A status line will appear in the footer of the page with the name of the file and the line you are on. Remember the line number for Step 3.

2. Press Shift-G to move to the end of the file.

3. Enter the page number you were on and then Shift-G. This will return you to the line you were when you pressed Ctrl-g. (When you enter the numbers, they are NOT shown on the screen.)

4. If you are confident to do this, perform steps 1 to 3.

The SEARCH COMMAND (SEARCH)

** Type / followed by a string to search for the string. **

1. In Normal Mode, type the / character. Note that it and the cursor appear at the bottom of the screen, like the command:.

2. Now type ‘test[RETURN]. This is the word you want to look for.

3. To search for the same word again, simply press n. To search for the same word in the opposite direction, press Shift-N.

4. If you want to search for a string in the opposite direction, use the? instead of / .

—> When the search reaches the end of the file it will continue from the beginning.

SEARCH FOR PARENTHESE MARRIAGE

** Enter % to find a wedding of ) ,], or}. **

1. Position the cursor on any (, [, or {on the line below marked with >.

2. Now enter the % character.

3. The cursor should be in the parentheses or square brackets that match the first one.

4. Type % to move the cursor back to the first bracket or parentheses (per marriage).

WAY TO CORRECT ERRORS

** Type: s / old / new / g to replace ‘new’ with ‘old’. **

1. Move the cursor to the line below marked with >.

2. Type: / s / aa / a [RETURN]. Note that this command only changes the first occurrence on the line.

3. Now type: s / aa / a / g meaning replace globally on the line. This changes all occurrences on the line.

4. To change every occurrence of a string between two lines,
Type: #, # s / old / new / g where #, # are the numbers of the two lines.
Type: % s / old / new / g to change every occurrence in the whole file.

HOW TO EXECUTE AN EXTERNAL COMMAND

** Type it :! Followed by an external command to execute this command. **

1. Enter the familiar command: to move the cursor to the screen footer. This allows you to enter a command.

2. Now enter the character ! (exclamation mark). This allows you to execute any shell command.

3. As an example type ls following the ! and then press [RETURN]. This will show you a listing of your directory as if you were at the shell prompt.

—> Note: You can execute any external command in this way.

—> Note: All commands: must be completed by typing [RETURN]

THE OPEN COMMAND (OPEN)

** Enter the to open a line under the cursor and put you in INSERT mode. **

1. Move the cursor to the line below marked with >.

2. Enter the (lowercase) to open a row UNDER THE Cursor and put you in Insert Mode (INSERT).

3. Now copy the line marked —> and press [ESC] to exit Insert Mode.

—> After typing o the cursor is placed on the open line in Insert Mode.

4. To open a line ABOVE the cursor, simply type a capital O rather than a lowercase one. Try this on the line below. Open a line above this by pressing Shift-O while the cursor is on this line.

THE APPEND COMMAND

** Enter a to enter text AFTER the cursor. **

1. Move the cursor to the end of the first line below marked with —> typing $ in Normal Mode.

2. Enter a (lowercase) to add text AFTER the character under the cursor. (The uppercase A adds to the end of the line.)

Note: This avoids typing i, the last character, the text to insert, [ESC], cursor to and left, and finally, x, only to add to the end of a line!

3. Now complete the first line, also note that append is exactly the same as insertion mode, except for where the text will be inserted.

—> This line allows you to practice
—> This line allows you to practice adding text to the end of the line.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>