VIM Cursor Movement & Edit Commands in MacOS
By: Karan Verma
Tags: Programming, Apple, Macos, Vim, Shell

1. Moving around in file

Move the cursor using combinations of the up, down, right, and left arrow keys.

a. Moving One Word

Press w (“word”) to move the cursor to the right one word at a time.

(“word”) to move the cursor to the at a time. Press b (“back”) to move the cursor to the left one word at a time.

(“back”) to move the cursor to the at a time. Press W or B to move the cursor past the adjacent punctuation to the next or previous blank space.

to move the cursor past the adjacent punctuation to the next or previous blank space. Press e (“end”) to move the cursor to the last character of the current word.

b. Moving to Start or End of Line

Press ^ to move the cursor to the start of the current line .

to move the cursor to the . Press $ to move the cursor to the end of the current line.

c. Moving to the Top of file

Press H (“high”) to move the cursor to the top of the screen.

d. Moving to the Middle of file

Press M (“middle”) to move the cursor to the middle of the screen.

e. Moving to the Bottom of file

Press L (“low”) to move the cursor to the bottom of the screen.

2. Inserting Text

vi provides many commands for inserting text. Each of these commands places vi in entry mode. To use any of these commands, you must first be in command mode. Remember to press Esc to make sure you are in command mode.

a. Append

Type a (append) to insert text to the right of the cursor .

(append) to insert text to the . Type A to add text to the end of a line.

b. Insert

Press i to insert text to the left of the cursor .

to insert text to the . Press I to insert text at the beginning of a line.

c. Open Line

Use these commands to open new lines, either above or below the current cursor position.

Type o to open a line below the current cursor position.

to open a line the current cursor position. Type O to open a line above the current cursor position.

3. Changing Text

Changing text involves the substitution of one section of text for another. vi has following ways to do this:

a. Changing a Word

Press cw to replace a word, followed by the new word. To finish, press Esc.

To change some part of a word, place the cursor on the word, to the right of the portion to be saved. Type cw, type the correction, and press Esc.

b. Changing a Line

Press cc to replace a line. The line disappears, leaving a blank line for your new text. Press Esc to finish.

c. Substituting Character(s)

Press s to substitute one or more characters for the character under the cursor, followed by the new text. Press Esc to return to command mode.

d. Replacing One Character

Press r to replace the character highlighted by the cursor with another character, followed by just one replacement character.

After the substitution, vi automatically returns to command mode (you do not need to press Esc).

4. Undoing Changes

When you edit text and make changes to a vi file, you might occasionally wish that you had not changed something. vi’s undo commands enable you to back up one operation and continue on from there.

a. Undoing the Previous Command

Press u to undo a change which you did previously.

b. Undoing Changes to a Line

Press U to undo all changes you’ve made to a line.

5. Deleting Text

These vi commands delete the character, word, or line you indicate. vi stays in command mode, so any subsequent text insertions must be preceded by additional commands to enter entry mode.

a. Deleting One Character

Press x to delete one character where the cursor is presently.

to delete one character where the cursor is presently. Press X to delete one character before (to the left of) the cursor.

b. Deleting a Word or Part of a Word

Press dw to delete a word. The word and the space it occupied are removed.

To delete part of a word, position the cursor on the word to the right of the part to be saved. Type dw to delete the rest of the word.

c. Deleting a Line

Press dd to delete a line.

6. Copying and Moving Text — Yank, Delete, and Put

The methods for copying or moving small blocks of text in vi involves the use of a combination of the yank, delete, and put commands.

a. Copying Lines

Press yy or Y to copy the line. Note that Y does the same thing as yy.

to the line. Note that Y does the same thing as yy. Press p to paste the line below the cursor

to the cursor Press P to paste the line above the cursor

to the cursor Copy multiple lines using 5yy (where 5 indicates to copy 5 lines)

The yy command works well with a count: to yank 5 lines, for example, type 5yy. Five lines, counting down from the cursor, are yanked, and vi indicates this with a message at the bottom of the screen: 5 lines yanked.

You can also use the P or p commands immediately after any of the deletion commands discussed earlier. This action puts the text you deleted above or below the cursor, respectively.

b. Moving Lines