How Can I Learn to Use vi Last modified: March 1, 1994 The vi editor can be a source of great confusion for many beginners on Unix systems. Here is a brief summary of the commands available in command mode in vi: VI COMMANDS This summary of commands provides an alphabetical list of editor commands. add text after the cursor back to the previous word replace all the characters in the line replace a word or the remaining characters in a word with new text
delete the current line delete the word delete the current sentence delete the current paragragh move the cursor forward to the last character in the next word move the cursor to the right to the specified character x create text in front of the cursor move the cursor one character to the left move the cursor down one line moves the cursor up one line moves the cursor one character to the right repeat the last search command create text at the beginning of a new line below the current line

place the contents of the temporary buffer containing the text obtained from teh most recent delete or yank command into the text after the cursor replace the current character delete the character the cursor in on and append text. move the cursor to the right to the character just before the specified character x undo the last command move the cursor forward to the next word deletes one character yank a line of text and place it into a temporary buffer <0> puts the cursor on the first character of a line <#> repeats command '#' of times <.> repeat the last command <$> puts the cursor on the last character of a line <^> puts the cursor on the first nonblank character of a line <-> moves the cursor up a line <+> moves the cursor down a line <;> continue searching in the same direction on the line for the last character requested with , , , or <,> continue searching in opposite direction on the line for the last character requested with , , , or <~> change lowercase to uppercase, or vice versa <(> move the cursor to beginning of current sentence <)> move the cursor to the beginning of the next sentence <{> move the cursor to the beginning of the current paragraph <}> move the cursor to the beginning of the next paragraph create text at the end of the current line ignore all punctuation and move the cursor back to the previous word replace the remaining characters in the line, from the cursor to the end of the line delete the portion of the line to the right of the cursor ignores all punctuation and moves cursor forward to the last character in the word after the next blank moves the cursor to the left to the specified character x go to the last line of the file go to the nth line of the file move the cursor to the first line on the screen create text before the first character on the current line that is not a blank join the line below the current line with the current line move the cursor to last line on the screen move the cursor to the middle line on the screen repeat the search command in the opposite direction create text at the beginning of a new line above the current line replace only those characters typed over with new characters until the is pressed replace all the characters in the line move the cursor to the left, to the character just after the specified character x ignore all punctuation and moves to the next word restore current line to its previous state writes buffer to file and return to shell <"xp> place the contents of the register -x after teh cursor <"xyn> place a copy of a text object of type n in the register named by the letter x scrolls the screen back a full window scrolls down a half screen scrolls the text forward one full window give the line number and file status clear and redraw the current window scrolls up a half screen of text moves the cursor down a line return VI to the command mode from any of the text input modes moves the cursor one character to the right : shows that the commands that follow are line editor commands :# goes to the nth line of the buffer :q leaves the editor and returns to the shell :q! quit VI without writing buffer to a file :r shell.file inserts the contents of shell.file after the current line of the buffer :w writes the buffer to a file :$ goes to the last line of the buffer :sh temporarily returns you to the shell to perform shell commands :x,yw data writes lines from the number x through the number y into a new file (data). :.,$d deletes all the lines in the buffer from the current line to the last line :s/text/new_words/ replaces the first instance of the characters text on the current line with new_words :s/text/new_words/g replaces every occurrence of text on the current line with new_words :g/text/s//new_words/g replaces every occurrence of text in the file with new_words /pattern search forward in the buffer for the next occurence of the pattern. ?pattern search backward in the buffer for the first occurence of the pattern vi -r file restores the changes made to file before interruption SHELL COMMAND LANGUAGE -------------------------- METACHARACTER: a subset of the special character that represent other characters 1) Asterisk(*): matches any string of characters, including a null string 2) Question mark(?): matches any single character of a file name 3) Bracket([]): Use brackets ([]) when you want the shell to match any one of several possible characters that may appear in one position in the file name SPECIAL CHARACTERS: The shell language has special characters that perform a variety of useful functions 1) Ampersand(&): Used to execute commands in background mode, thus freeing your terminal for other tasks: command & 2) Semicolon(;): You can type two or more commands on one line as long as each pair is separated by a semicolon(;) 3) Backslash(/): The shell interprets the backslash as an escape character that allows you to turn off any special meaning of the character immediately after it. 4) Quotes: Single quotes ('...') turn off the special meaning of any character. Double quotes ("...") turn off the special meaning of all character except $ and `, which retain their special meanings with double quotes. A common use of quotes as escape characters is for turning off the special meaning of the blank space. INPUT AND OUTPUT REDIRECTION: The UNIX system lets you reassign the standard input and output to other files and programs, known as redirection. 1) Redirecting Input: the < Sign: To redirect input, specify a file name after a less than sign (<) on a command line: command < file 2) Redirecting Output to a File: the > Sign: To redirect output, specify a file name after the greater than sign (>) on a command line: command > file 3) Appending Output to a File: the >> Symbol: To keep from destroying an existing file, you can also use the double redirection symbol (>>), as follows: command >> filename , which appends the output of a command to the end of the file filename. 4) Redirecting Output to a Command: the Pipe(|): Pipes are powerful tools that allow you to take the output of one command and use it as input for another command without creating temporary files. 5) Substituting Output for an Argument: The output of any command may be captured and used as arguments on a command line. This is done by enclosing the command in grave accents (`...`) and placing it on the command line in the position where the output should be treated as arguments. EXECUTING AND TERMINATING PROCESSES 1) Running Command with BATCH and AT The batch and at commands allow you to specify a command or sequence of command to be run at a later time. With the BATCH command, the system determines when the commands run; with the AT command, you determine when the commands run. 2) Terminating Active Processes The KILL command is used to terminate active shell processes: KILL PID 3) Using the NOHUP Command If you want a background process to continue running after you log off, you must use the NOHUP command to submit that background command: NOHOUP command & UNIX MAIL COMMAND ------------------- # display message number. - print previous. + next (no delete). ! cmd execute cmd. a position at and read newly arived mail. dq delete current message and exit. d[#] delete message. ha display all headers. hd display headers of letters scheduled for deletion. h[#] display headers around # (default current message). m user mail (and delete) current message to user. n next (no delete). p print. q quit. r[args] reply to (and delete) current letter via mail[args]. s[file] save (and delete) current message (default mbox). u[#] undelete message # (default current message). w[file] save (and delete) current message without header. x exit without changing mail. y[file] save (and delete) current message (default mbox). BASIC UNIX SYSTEM COMMANDS ------------------------------ AT - Request that a command be run in background mode at a time you specify on the command line. BANNER - Display a message (in words up to 10 characters long) in large letters on the standard output. BATCH - Submit command(s) to be processed when the system load is at an acceptable level. CAT - Display the contents of a specified file at your terminal. CD - Change directory from the current one to your home directory. If you include a directory name, the directory will change from the current one to the directory specified. CP - Copy a specified file into a new file, leaving the original file intact. CUT - Cut out specified fields from each line of a file. DATE - Display the current date and time. DIFF - Compare two files. ECHO - Display input on the terminal, including the carriage return, and return a prompt. ED - Edit a specified file using the line editor. GREP - Search a specified file(s) for a pattern and print those lines that contain the pattern. KILL - Terminate a background process specified by its process identification number (PID). LEX - Generate programs to be used in simple lexical analysis of text, perhaps as a first step in creating a compiler. LP - Print out the contents of a specified file on a line printer. LPSTAT - Display the status of any requests made to the line printer. LS - List the names of all files and directories except those whose names begin with a dot (.). MAIL - Display any electronic mail you may have received at your terminal, one message at a time. MAKE - Maintain and support large programs or documents on the basis of smaller ones. MKDIR - Make a new directory. MV - Move a file to a new location in the file system. You can move a file to a new file name. NOHUP - Place execution of a command in the background, so it will continue executing after you log off the system. PG - Display the contents of a specified file on your terminal, a page at a time. PR - Display a partially formatted version of a specified file at your terminal. PS - Display the status and number of every process currently running. PWD - Display the full pathname of the current working directory. RM - Remove a file from the file system. RMDIR - Remove a directory. SORT - Sort a file in ASCII order and display the results on your terminal. SPELL - Collect words from a specified file and check them against a spelling list. Words not on the list or not related to words on the list are displayed. UNAME - Display the name of the UNIX system on which you are currently working. UUCP - Send a specified file to another UNIX system. UUNAME - List the names of remote UNIX systems that can communicate with your UNIX system. UUPICK - Search the public directory for files sent to you be the UUTO command. UUSTAT - Report the status of the UUTO command you issued to send files to another user. UUTO - Send a specified to another user. VI - Edit a specified file using the VI screen editor. WC - Count the number of lines, words, and characters in a specified file and display the results on your terminal. WHO - Display the login names of the users currently logged in on your UNIX system. YACC - Impose a structure on the input of a program. .