Vi Commands
Source: https://www.cs.colostate.edu/helpdocs/vi.html
<HTML> <H3>What is <TT>vi</TT>?</H3> <DL> <DD>The default editor that comes with the UNIX operating system is called <TT>vi</TT> (<B>vi</B>sual editor). [Alternate editors for UNIX environments include <TT>pico</TT> and <TT>emacs</TT>, a product of GNU.] <P> <DD>The UNIX <TT>vi</TT> editor is a full screen editor and has two modes of operation:
<OL> <LI> <I>Command mode</I> commands which cause action to be taken on the file, and <LI><I>Insert mode</I> in which entered text is inserted into the file. </OL>
In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the <TT>vi</TT> editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the <TT><Esc></TT> (<I>Escape</I>) key turns off the Insert mode. <P> <DD>While there are a number of <TT>vi</TT> commands, just a handful of these is usually sufficient for beginning <TT>vi</TT> users. To assist such users, this Web page contains a sampling of basic <TT>vi</TT> commands. The most basic and useful commands are marked with an asterisk (<TT>*</TT> or star) in the tables below. With practice, these commands should become automatic. <P> <DD><B>NOTE:</B> Both UNIX and <TT>vi</TT> are <B>case-sensitive</B>. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect. </DL> <P>
<P> <H3>To Get Into and Out Of <TT>vi</TT></H3> <P> <H4>To Start <TT>vi</TT></H4> <DL> <P> <DD>To use <TT>vi</TT> on a file, type in <TT>vi filename</TT>. If the file named <TT>filename</TT> exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text. </DL> <P> <CENTER> <TABLE BORDER> <TH> * <TH ALIGN=LEFT><TT>vi filename</TT>
<TD> <I>edit <TT>filename</TT> starting at line 1</I>
<TR> <TH>   <TH ALIGN=LEFT><TT>vi -r filename</TT>
<TD> <I>recover <TT>filename</TT> that was being edited when system crashed</I>
<TR> </TABLE> </CENTER> <P> <H4>To Exit <TT>vi</TT></H4> <P> <DL> <DD>Usually the new or modified file is saved when you leave <TT>vi</TT>. However, it is also possible to quit <TT>vi</TT> without saving the file. <P> <DD><B>Note:</B> The cursor moves to bottom of screen whenever a colon (<TT>:</TT>) is typed. This type of command is completed by hitting the <TT><Return></TT> (or <TT><Enter></TT>) key. </DL> <P> <CENTER> <TABLE BORDER> <TR> <TH> * <TH ALIGN=LEFT><TT>:x</TT><TT><Return></TT>
<TD> <I>quit <TT>vi</TT>, writing out modified file to file named in original invocation</I>
<TR> <TH>   <TH ALIGN=LEFT><TT>:wq</TT><TT><Return></TT>
<TD> <I>quit <TT>vi</TT>, writing out modified file to file named in original invocation</I>
<TR> <TH>   <TH ALIGN=LEFT><TT>:q</TT><TT><Return></TT>
<TD> <I>quit (or exit) <TT>vi</TT></I>
<TR> <TH> * <TH ALIGN=LEFT><TT>:q!</TT><TT><Return></TT>
<TD> <I>quit <TT>vi</TT> even though latest changes have not been saved for this <TT>vi</TT> call</I>
<TR> </TABLE> </CENTER> <P>
<P> <H3>Moving the Cursor</H3> <DL> <P> <DD>Unlike many of the PC and MacIntosh editors, <B>the mouse does not move the cursor</B> within the <TT>vi</TT> editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since <TT>vi</TT> was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in <TT>vi</TT> and should be avoided. <P> <DD>If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two. <P> <DD>In the table below, the symbol <TT>^</TT> before a letter means that the <TT><Ctrl></TT> key should be held down while the letter key is pressed. </DL> <P> <CENTER> <TABLE BORDER> <TH> * <TH ALIGN=LEFT><TT>j</TT> <I>or</I> <TT><Return></TT>
<BR>   [<I>or</I> down-arrow] <TD> <I>move cursor down one line</I>
<TR> <TH> * <TH ALIGN=LEFT><TT>k</TT> [<I>or</I> up-arrow]
<TD> <I>move cursor up one line</I>
<TR> <TH> * <TH ALIGN=LEFT><TT>h</TT> <I>or</I> <TT><Backspace></TT>
<BR>   [<I>or</I> left-arrow] <TD> <I>move cursor left one character</I>
<TR> <TH> * <TH ALIGN=LEFT><TT>l</TT> <I>or</I> <TT><Space></TT>
<BR>   [<I>or</I> right-arrow] <TD> <I>move cursor right one character</I>
<TR> <TH> * <TH ALIGN=LEFT NOWRAP><TT>0</TT> (zero)
<TD> <I>move cursor to start of current line (the one with the cursor)</I>
<TR> <TH> * <TH ALIGN=LEFT NOWRAP><TT>$</TT>
<TD><I>move cursor to end of current line</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>w</TT>
<TD><I>move cursor to beginning of next word</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>b</TT>
<TD><I>move cursor back to beginning of preceding word</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:0</TT><TT><Return> <I>or</I> 1G</TT>
<TD> <I>move cursor to first line in file</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:n</TT><TT><Return> <I>or</I> nG</TT>
<TD> <I>move cursor to line <TT>n</TT></I>
<TR> <TH>   <TH ALIGN=LEFT><TT>:$</TT><TT><Return> <I>or</I> G</TT>
<TD> <I> move cursor to last line in file</I>
<TR> </TABLE> </CENTER> <P>
<P> <H3>Screen Manipulation</H3> <DL> <P> <DD>The following commands allow the <TT>vi</TT> editor screen (or window) to move up or down several lines and to be refreshed. </DL> <P> <CENTER> <TABLE BORDER> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^f</TT>
<TD> <I>move forward one screen</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^b</TT>
<TD> <I>move backward one screen</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^d</TT>
<TD> <I>move down (forward) one half screen</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^u</TT>
<TD> <I>move up (back) one half screen</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^l</TT>
<TD> <I>redraws the screen</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^r</TT>
<TD> <I>redraws the screen, removing deleted lines</I>
<TR> </TABLE> </CENTER> <P>
<P> <H3>Adding, Changing, and Deleting Text</H3> <DL> <P> <DD>Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables. <P> <DD>Perhaps the most important command is the one that allows you to back up and <I>undo</I> your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step. </DL> <P> <CENTER> <TABLE BORDER> <TH> * <TH ALIGN=LEFT NOWRAP><TT>u</TT>
<TD> <I>UNDO WHATEVER YOU JUST DID; a simple toggle</I>
<TR> </TABLE> </CENTER> <DL> <P> <DD>The main purpose of an editor is to create, add, or modify text for a file. <P> </DL> <H4>Inserting or Adding Text</H4> <DL> <P> <DD> The following commands allow you to insert and add text. Each of these commands puts the <TT>vi</TT> editor into insert mode; thus, the <TT><Esc></TT> key must be pressed to terminate the entry of text and to put the <TT>vi</TT> editor back into command mode. <P> </DL> <CENTER> <TABLE BORDER> <TH> * <TH ALIGN=LEFT NOWRAP><TT>i</TT>
<TD> <I>insert text before cursor, until <TT><Esc></TT> hit</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>I</TT>
<TD> <I>insert text at beginning of current line, until <TT><Esc></TT> hit</I>
<TR> <TH> * <TH ALIGN=LEFT NOWRAP><TT>a</TT>
<TD> <I>append text after cursor, until <TT><Esc></TT> hit</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>A</TT>
<TD> <I>append text to end of current line, until <TT><Esc></TT> hit</I>
<TR> <TH> * <TH ALIGN=LEFT NOWRAP><TT>o</TT>
<TD> <I>open and put text in a new line below current line, until <TT><Esc></TT> hit</I>
<TR> <TH> * <TH ALIGN=LEFT NOWRAP><TT>O</TT>
<TD> <I>open and put text in a new line above current line, until <TT><Esc></TT> hit</I>
<TR> </TABLE> </CENTER> <P> <H4>Changing Text</H4> <DL> <P> <DD>The following commands allow you to modify text. <P> </DL> <CENTER> <TABLE BORDER> <TH> * <TH ALIGN=LEFT NOWRAP><TT>r</TT>
<TD> <I> replace single character under cursor (no <TT><Esc></TT> needed)</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>R</TT>
<TD> <I>replace characters, starting with current cursor position, until <TT><Esc></TT> hit</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>cw</TT>
<TD> <I>change the current word with new text, <BR>starting with the character under cursor, until <TT><Esc></TT> hit</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>cNw</TT>
<TD> <I>change <TT>N</TT> words beginning with character under cursor, until <TT><Esc></TT> hit; <BR>   e.g., <TT>c5w</TT> changes 5 words</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>C</TT>
<TD> <I>change (replace) the characters in the current line, until <TT><Esc></TT> hit</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>cc</TT>
<TD> <I>change (replace) the entire current line, stopping when <TT><Esc></TT> is hit</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>Ncc</TT> <I>or</I> <TT>cNc</TT>
<TD> <I>change (replace) the next N lines, starting with the current line,<BR> stopping when <TT><Esc></TT> is hit</I>
<TR> </TABLE> </CENTER> <P> <H4>Deleting Text</H4> <DL> <P> <DD>The following commands allow you to delete text. <P> </DL> <CENTER> <TABLE BORDER> <TH> * <TH ALIGN=LEFT NOWRAP><TT>x</TT>
<TD> <I>delete single character under cursor</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>Nx</TT>
<TD> <I>delete N characters, starting with character under cursor</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>dw</TT>
<TD> <I>delete the single word beginning with character under cursor</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>dNw</TT>
<TD> <I>delete <TT>N</TT> words beginning with character under cursor; <BR>   e.g., <TT>d5w</TT> deletes 5 words</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>D</TT>
<TD> <I>delete the remainder of the line, starting with current cursor position</I>
<TR> <TH> * <TH ALIGN=LEFT NOWRAP><TT>dd</TT>
<TD> <I>delete entire current line</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>Ndd</TT> <I>or</I> <TT>dNd</TT>
<TD> <I>delete <TT>N</TT> lines, beginning with the current line; <BR>   e.g., <TT>5dd</TT> deletes 5 lines</I>
<TR> </TABLE> </CENTER> <P> <H4>Cutting and Pasting Text</H4> <DL> <P> <DD>The following commands allow you to copy and paste text. <P> </DL> <CENTER> <TABLE BORDER> <TH>   <TH ALIGN=LEFT NOWRAP><TT>yy</TT>
<TD> <I>copy (yank, cut) the current line into the buffer</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>Nyy</TT> <I>or</I> <TT>yNy</TT>
<TD> <I>copy (yank, cut) the next N lines, including the current line, into the buffer</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>p</TT>
<TD> <I>put (paste) the line(s) in the buffer into the text after the current line</I>
<TR> </TABLE> </CENTER> <P>
<P> <H3>Other Commands</H3> <P> <H4>Searching Text</H4> <DL> <P> <DD> A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands. <P> </DL> <CENTER> <TABLE BORDER> <TR> <TH>   <TH ALIGN=LEFT><TT>/string</TT>
<TD> <I> search forward for occurrence of <TT>string</TT> in text<I>
<TR> <TH>   <TH ALIGN=LEFT><TT>?string</TT>
<TD> <I> search backward for occurrence of <TT>string</TT> in text<I>
<TR> <TH>   <TH ALIGN=LEFT><TT>n</TT>
<TD> <I> move to next occurrence of search string</I>
<TR> <TH>   <TH ALIGN=LEFT><TT>N</TT>
<TD> <I> move to next occurrence of search string in opposite direction</I>
<TR> </TABLE> </CENTER> <P> <H4>Determining Line Numbers</H4> <DL> <P> <DD> Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful. <P> </DL> <CENTER> <TABLE BORDER> <TH>   <TH ALIGN=LEFT><TT>:.=</TT>
<TD> <I> returns line number of current line at bottom of screen<I>
<TR> <TH>   <TH ALIGN=LEFT><TT>:=</TT>
<TD> <I> returns the total number of lines at bottom of screen<I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>^g</TT>
<TD> <I>provides the current line number, along with the total number of lines,<BR> in the file at the bottom of the screen</I>
<TR> </TABLE> </CENTER> <P>
<P> <H3>Saving and Reading Files</H3> <P> These commands permit you to input and output files other than the named file with which you are currently working. <P> <CENTER> <TABLE BORDER> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:r filename</TT><TT><Return></TT>
<TD> <I> read file named <TT>filename</TT> and insert after current line <BR>(the line with cursor)</I>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:w</TT><TT><Return></TT>
<TD> <I>write current contents to file named in original <TT>vi</TT> call
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:w newfile</TT><TT><Return></TT>
<TD> <I>write current contents to a new file named <TT>newfile</TT>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:12,35w smallfile</TT><TT><Return></TT>
<TD> <I>write the contents of the lines numbered 12 through 35 to a new file named <TT>smallfile</TT>
<TR> <TH>   <TH ALIGN=LEFT NOWRAP><TT>:w! prevfile</TT><TT><Return></TT>
<TD> <I>write current contents over a pre-existing file named <TT>prevfile</TT>
<TR> </TABLE> </CENTER> </HTML>