vi Commands The vi text editor is the primary tool you'll use to create shell scripts. Starting Up When you start vi up, you should also specify the name of a file to work on, for example $ vi thisfile The vi text editor starts up, by default, in command mode. Cursor Navigation Most importantly, you want to "navigate the file" (move your cursor around): The j command moves the cursor down one line The k command moves the cursor up one line The l command moves the cursor right one character The h command moves the cursor left one character The w command moves the cursor to the beginning of the next word The b command moves the cursor to the beginning of the previous word Note: do not use the arrow, home, end, and other keys on the right section of your keyboard. Use only the vi navigation commands. Use the following commands to move the cursor. The G command moves the cursor to the last line in the file. The 0 command moves the cursor to the beginning of the current line. The $ command moves the cursor to the end of the current line. You can search for text by using the / command, followed by the text you want the vi editor to find. For example /string moves the cursor to the next location of the word string. Enter Text The vi editor makes a lot of commands available, but usually, to begin, you want to use a command to put vi in text entry mode so you can start entering text. The i command (insert): start typing at the current cursor position. The a command (append): start typing after the current cursor position. To get out of text entry mode and back to command mode, hit the ESC (escape) key. Then you can use the bulk of vi's commands. Edit Text You may want to delete some text: The x command deletes the character under the cursor Use the following commands to cut and paste a line. The yy command copies the current line to the standard buffer. The p command pastes the contents of the standard buffer to the point after the cursor. Use the following commands to delete a line or a word. The dd command deletes the current line (use D to delete remainder of line). The dw command deletes the current word from the cursor position to the word's end. Use the following commands to change a letter or word while keeping vi in command mode. The r command converts a letter to another letter The cw command converts a word to another word Save and Quit Usually you want to save your work, so you want to put vi in its file-oriented mode: type a colon ( : )(when vi is in command mode). A colon appears at the bottom of the screen, letting you enter file-oriented commands: The w command "writes" your work to a file The q command lets you quit using the vi program The wq command writes your work and quits vi Automatically Repeat a Command Use numbers to amplify a command. The j, k, l, h, x, G, yy, dd, and dw commands all take integer prefix arguments to specify multiple repeated operations. For example, 10j moves the cursor down ten lines. 5k moves the cursor up five lines. 3x deletes the current character as well as the next two characters. 18G moves the cursor to line 18. 7dw deletes the current word as well as the next six words.