Basic vi Concepts
Learning how to use the basic vi concepts
A user on vi is in one of three modes - command mode, insert mode, or last line mode. When the vi command is invoked with a filename argument, the user is initially in command mode. In this mode, the user may enter commands or change to other modes but not actually modify the file. For example, typing 'dd' in command mode does not insert these characters; it simply deletes the line that the cursor is currently positioned on. The arrow keys move the cursor around the file. Most vi commands are one or two characters long.
Note: All vi commands are case sensitive
- Type the following prompt to get into the vi editor to edit backup.sh:
Screenshot 9a: Vi: New File Edition
[ LinuxUser ] ~$ vi backup.sh
- The column of '~' characters indicates the end of the file. Since this is a new file; the end of the file is at the very beginning.
- To get into text mode to actually edit the file, type 'i'. The 'i' command inserts text before the current character (the character under which the cursor is positioned) while the 'a' command appends text after the current character. Now, type the following line and hit enter to add a line break. Just use the backspace key (not del or any other key - these may get you to command mode) to correct errors:
#!/bin/sh
Screenshot 9b: Vi: A new line
- Hit the 'esc' key. This will get you back to command mode. 'Esc' always gets a user out of edit mode into command mode. Try using the arrow keys to move around. If you try moving beyond the document boundaries (e.g. up or down and the file has just one line), you may hear a beep.
- Type a colon. You will see the cursor at the bottom of the screen. You are now in last line mode. Extended commands may be issued from this mode by typing them after the colon and pressing enter. Typing a colon in the command mode always gets the user to the last line mode.
- Three options are available to quit a file in last line mode. Just 'q' quits if modifications have not been made during the vi session (if any modifications have been made, an error is displayed and the user is not allowed to quit via 'q'). The command 'q!' quits without saving changes while the command 'wq' quits after saving changes.
- Issue the 'wq' command after the ':' and hit enter. This will take you back to the command prompt.

Screenshot 9c: Vi: Last line mode
- Issue the command 'vi backup.sh'. The file will load again. You will see the line you entered the last time. Type 'o'. This will take you into edit mode and place the cursor at the start of the next line. Enter the following lines:
# Directories to backup SOURCES="/home /var/mail /etc" # Directory to backup to. # Filesystem Reference to mounted usb drive TARGET="/mnt/usb-harddrive/"
Screenshot 9d: Vi: Add more lines
- Hint: You may copy the above lines by highlighting them and using the 'Edit' and 'Copy' buttons on the menu with your mouse; your Xwindows installation will allow you to paste the lines directly into the vi program (once again using menu items edit-paste ) while in edit mode. However, you have to be in edit mode for the text to get pasted correctly; otherwise, vi will assume that you are entering commands
- Press 'esc' to get to command mode. Move around using the arrow keys. You may also move around using the h, j, k, and l keys to move the cursor left, down, up, and right, respectively. Try moving around using h, j, ,k , and l. Go to the final line again and type 'o' from anywhere in the final line.
- Type a couple of lines of junk and press esc again.
- Use the 'dd' command to delete the lines of junk. Simply position the cursor anywhere in the line and type 'dd' (You may also use 'x' to delete a single character. Typing 'dw' will delete the current word )
- Typing 'r' will allow a user to replace a single character and push him right back to command mode. Typing 'R' (upper case) will get the user into edit mode and allow him to overwrite text starting at the current position . The '~' character changes the case of the current character.
- The '0' (zero) and '$' commands move the cursor to the start and end of a line.
- Typing 'G' takes the user to the end of the file. Typing a number before 'G' will take the user to the line of the file indicated by the number. Type '3G'. Your cursor will be positioned at the start of line 3 (Sources= etc.)
- Commands may be combined. 'd$' deletes everything from the current position to the end of the line; dG deletes everything from the current position to the end of the file.
- The 'J' key may be used to join the contents of two lines.
- Get into last line mode by typing ':'. Enter 'w'. This saves the file without quitting vi.
- You may now edit another file by going to last line mode again by typing ':' and entering 'e <file_name>'. Try ':e newfile.txt' (don't type another colon if you are already in last line mode).
- Switch back to backup.sh by typing ':e backup.sh'. Thus, you may switch between multiple files in vi.
- Switch back to newfile.txt by issuing ':e newfile.txt'
- Note that you are always in command mode when you enter vi.
- Now, type ':' to get to the last line mode and type 'r backup.sh'. This command causes the contents of backup.sh to be inserted into the current file.
- Go to the last character on the last line (use the down arrow key or 'j' to move down and '$' to move to the end of the line.
- You may also use the 'r!' command to insert the output of a shell command at the cursor position. Try ':r! ls -l'. Your newfile.txt will now look like this:
Screenshot 9e: Vi: Add other files and command output
- Switch to backup.sh without saving changes by typing ':e! backup.sh'
- To just view the output of a command without including its contents, you may type '! <command>' in the last line mode. Type ':' and then '! ls -l'. This will show the contents of the current directory; you will have to press the enter key to get back to vi. Any command may be executed in this manner
- You may type 'shell' in last line mode. The vi editor starts an instance of the shell and goes on hold temporarily while you execute other commands. To get back to editing, just type exit.
Now that you have learned a little bit about vi, you should be able to add the rest of the backup.sh file's contents and save the file on your system. Save the file and execute it after customization to create a backup.
Vi uses buffers to store deleted text. There are nine numbered buffers (1-9) as well as an undo buffer. Buffer 1 contains the most recent deletion, buffer 2 the next recent and so on. To cut and paste in vi, delete the text (5dd will delete five lines and so on). Move to the line where you want the text to appear and press p. If something else has been deleted before pasting, type '2p' to get the contents of the previous delete buffer. '5yy' copies five lines (6yy copies 6 lines and so on). You may use 'p' to paste copied text.
Typing a '/' in the vi editor will take you to a variant of the last line mode called search mode. You may enter strings or regular expressions; the cursor will go to the first occurrence of the pattern or string. To search and replace all occurrences of pattern1 with pattern2, use ':%s/pattern1/pattern2/g'. Vi prompts for confirmation before each replacement if you add a 'c' to this substitution command and use ':%s/pattern1/pattern2/gc' .
vi Reference
| Text Insertion | |
| i | insert text (and enter input mode) |
| $a | append text (to end of line) |
| ESC | re-enter command mode |
| J | join lines |
| Cursor movement: | |
| h | left |
| j | down |
| k | up |
| l | right |
| ^ | beginning of line |
| $ | end of line |
| 1 G | top of document |
| G | end of document |
| G | go to line |
| ^F | page forward |
| ^B | page backward |
| w | word forwards |
| b | word backwards |
| Text Deletion/Moving | |
| Backspace | delete character before cursor(only works in insert mode) |
| x | delete character under cursor |
| dw | delete word |
| dd | delete line (restore with p or P) |
| dd | delete n lines |
| d$ | delete to end of line |
| dG | delete to end of file |
| yy | copy line (restore with p or P) |
| n yy | copy n lines |
| Search and replace | |
| %s/<search_string>/<replace_string>/g | |
| Miscellaneous | |
| u | undo |
| :w | save file |
| :wq | save file and quit |
| ZZ | save file and quit |
| :q! | quit without saving |