\chapter{Introduction to Editing With {\pgn vi}}
\label{cha:intro2vi}
{\mns



\subsection{Objectives}

In this section, you will learn how to:

\begin{itemize}
  
\item Use the {\pgn vi} editor to view, create and edit files
\begin{itemize}
  \item the {\cmdn vi} screen layout
  \item move round in files
  \item replace, insert and change text
  \item search files
\end{itemize}

\end{itemize}
 


\section{Text editors under Linux}

\begin{itemize}
  \item There are a number of text-editors available
  \item {\pgn vi} is on virtually every Linux distribution
  \item Also comes with 99\% of Unix systems
  \item Everyone should have a basic understanding
  \item {\pgn vi} is like Linux
  \begin{itemize}
    \item Has some very complex and powerful functions that can make your life easier
    \item However, you don't {\em have} to know everything; you get by knowing the basics
    \item Shares key bindings with many utilities
  \end{itemize}
  \item We'll just cover the basics here, {\pgn vi} is too big to cover everything!
\end{itemize}



\section{{\pgn vi} and your terminal}

\begin{itemize}
  \item {\pgn vi} is fundamentally text-based
  \begin{itemize}
    \item Graphical adaptations {\em are} available ({\pgn gvim})
  \end{itemize}
  \item Needs to know your terminal's capabilities
  \begin{itemize}
    \item May not function if your terminal is misconfigured
    \item Check your TERM environment variable
    \item Terminal capabilities are listed in {\fn /etc/termcap}
    \item Generally not an issue except with Windows 98 telnet client.
      When use windows {\cmdn telnet}, always type {\cmdn export
        TERM=vt100} before starting {\cmdn vi} and other such
      programs, or they will not work properly. 
  \end{itemize}
\end{itemize}



\section{{\pgn vi} screen layout}
\label{sec:vi-screen layout}

\begin{itemize}
  \item Lines containing simply a \verb|~| show that you are past the end of the file and there is nothing here.
  \item The terminal's bottom line is the {\kwd status line}
  \begin{itemize}
  \item Shows status messages
  \item Where you type some commands
    
    (The `ed'/`ex' command set, explained later)
  \end{itemize}
  {\myts \begin{verbatim}
This is a test document
Some lines of text here

One, two, three
four, five, six
~
~
~
~
~
~
"test" 5 lines, 77 characters written
\end{verbatim}}
\end{itemize}



\section{Opening files with {\cmdn vi}}
\label{sec:opening-files-with-vi}

\begin{itemize}
  \item Launch {\pgn vi} by typing its name on command line
  \item With no arguments {\pgn vi} starts with an un-named and empty buffer
  \item {\pgn vi} {\fn filename} opens a specific file
  \item If you don't have write permission on a file the status line will tell you :
{\myss \begin{verbatim}
"/etc/aliases" [readonly] 152 lines, 3215 characters
\end{verbatim}}
  \item If there is no such file status line will say something like :
{\myss \begin{verbatim}
"some_filename" [New File]
\end{verbatim}}
\end{itemize}



\section{{\pgn vi} Modes}
\label{sec:vimodes}

\begin{itemize}
  \item Unlike many editors {\pgn vi} does not always insert what you type into the file
  \item Has several `modes'
  \begin{itemize}
    \item Only one is responsible for inserting text into the current file
  \end{itemize}
  \item {\pgn vi} has 3 modes:~\footnote{Some people refer to "ex" instead of "ed". They are the same thing}
\\ \bigskip
  {\myss
  \begin{tabular}{|l|>{\PBS\rr}p{216pt}|} \hline 
  command mode            & Moving the cursor, searching and
                            manipulating existing text \\ \hline
  insert mode             & Entering new text \\ \hline
  `:' (`ed') mode         & File manipulation, advanced 
                            searching and substitution \\ \hline
  \end{tabular}   }
\vspace{16pt}  
  \item {\pgn vi} starts in command mode
  \item Return to command mode at any time by hitting {\cmdn <ESC>}
\end{itemize}



\section{Saving, changing file and quitting}
\label{sec:saving-etc}

\begin{itemize}
  \item When you open a file, a copy of it is opened into memory
  \item Any changes you make apply to this copy {\em only}
  \item File on disk only changes if  you explicitly say so
  \item To save (or {\em write}) a file you must be in command-mode, then type {\cmdn :w}
  \item Can save your file under a new name, e.g.
    \begin{quote}
       {\cmdn :w newfilename}
    \end{quote}
  \item To quit {\cmdn vi} type {\cmdn :q}
  \item {\cmdn vi} normally prompts you if you have unsaved work
  \item To quit without saving your work type {\cmdn :q!}
  \item {\cmdn ZZ} will save your work and then quit
\end{itemize}



\section{Moving around in command mode}
\label{sec:moving-around}

\begin{itemize}
  \item Many ways to move around a document
  \item You must be in command mode for the following :
  \begin{itemize}
    \item On `friendly' terminals you can use arrow keys
    \item Arrow keys are sometimes unavailable on some terminals so {\pgn vi} has some alternatives
  \end{itemize}
  \begin{tabular}{p{90pt}p{30pt}p{30pt}p{30pt}}
&&k& \\
&h&&l \\
&&j& \\
\end{tabular}
  \item Although  `awkward' at first, these make your life easier
  \begin{itemize}
    \item Always work, regardless of system type
    \item Fingers stay on the `home' keys
  \end{itemize}
\end{itemize}



\section{Numeric Prefixes}
\label{sec:numeric-prefixes}

\begin{itemize}
  \item Key concept: `numeric prefixes' or `multipliers'
  \begin{itemize}
    \item Vastly improves the usefulness of many commands
  \end{itemize}
  \item To supply a prefix simply type the number before the command
  \begin{itemize}
    \item {\pgn vi} will then perform the command the specified number of times.
  \end{itemize}
  \item Note: In subsequent examples a small box indicates the position of the cursor
  \item Starting with \\ \bigskip
{\cmdn \framebox[10pt]{T}he quick brown\\fox jumped over\\the lazy dog}\\ \bigskip
and pressing {\cmdn 2l} will result in \\ \bigskip
{\cmdn Th\framebox[10pt]{e} quick brown\\fox jumped over\\the lazy dog}\\ \bigskip
\end{itemize}



\section{Further Movement}
\label{sec:further-movement}

\begin{itemize}
  \item {\pgn vi} also allows movements by units other than characters.
  \item Moving by pages : \\[12pt]
\begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline
{\cmdn \verb|^|f} & Forward one screenful \\ \hline
{\cmdn \verb|^|b} & Back one screenful \\ \hline
{\cmdn \verb|^|u} & Forward half a screenful \\ \hline
{\cmdn \verb|^|d} & Back half a screenful \\ \hline
\end{tabular}\\[12pt]
  \item Moving by `words' : \\[12pt]
\begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline
{\cmdn w} & Go to beginning of next word \\ \hline
{\cmdn e} & Go to end of next word \\ \hline
{\cmdn b} & Go to start of previous word \\ \hline
\end{tabular}\\[12pt]
  \item For these commands punctuation is not counted as part of a word
  \item The commands {\cmdn W}, {\cmdn E} and {\cmdn B} act the same but {\em do}
        include punctuation in words
  \item NOTE: Case is important to {\cmdn vi} commands, {\cmdn b} and {\cmdn B} are different commands!
  \begin{itemize}
    \item The upper and lower case versions of commands are {\em usually} related
  \end{itemize}
\end{itemize}



\section{Further Movement --- Example}
\label{sec:further-movement-examples}

\begin{itemize}
  \item From \\{\cmdn This, he \framebox[10pt]{s}aid, is an example} \\[12pt]
\begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn w} & {\cmdn This, he said\framebox[10pt]{,} is an example} \\ \hline
{\cmdn W} & {\cmdn This, he said, \framebox[10pt]{i}s an example} \\ \hline
{\cmdn e} & {\cmdn This, he sai\framebox[10pt]{d}, is an example} \\ \hline
{\cmdn E} & {\cmdn This, he said\framebox[10pt]{,} is an example} \\ \hline
{\cmdn b} & {\cmdn This, \framebox[10pt]{h}e said, is an example} \\ \hline
{\cmdn B} & {\cmdn This, \framebox[10pt]{h}e said, is an example} \\ \hline
\end{tabular}\\[12pt]
  \item As with virtually all commands these may be given a numeric prefix
  \item From the original start-point : \\[12pt]
\begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn 2w} & {\cmdn This, he said, \framebox[10pt]{i}s an example} \\ \hline
{\cmdn 2W} & {\cmdn This, he said, is \framebox[10pt]{a}n example} \\ \hline
{\cmdn 2e} & {\cmdn This, he said\framebox[10pt]{,} is an example} \\ \hline
{\cmdn 2E} & {\cmdn This, he said, i\framebox[10pt]{s} an example} \\ \hline
{\cmdn 2b} & {\cmdn This\framebox[10pt]{,} he said, is an example} \\ \hline
{\cmdn 2B} & {\cmdn \framebox[10pt]{T}his, he said, is an example} \\ \hline
\end{tabular}\\[12pt]
  \item It's not {\em necessary} to know these, but they make life a lot easier when you get used to them!
\end{itemize}





\section{Movement by lines}
\label{sec:movement-by-line}

\begin{itemize}
  \item What if we want to get to the {\em beginning} of the next line\footnote{A `line' is the set of characters contained between newline characters, not necessarily what appears on one line in your terminal}?
  \item Commands to move to line start/end:\\[12pt]
\begin{tabular}{|p{40pt}|p{293pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn \$} & Move to the end of the current line \\ \hline
{\cmdn 0} & Move to start of current line \\ \hline
{\cmdn \verb|^|} & Move to first character of line \\ \hline
\end{tabular} \\[12pt]
  \item Moving to start of a previous or subsequent line  \\[12pt]
\begin{tabular}{|p{60pt}|p{273pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn +} & Move to beginning of the next line \\ \hline
{\cmdn -} & Move to beginning of the previous line \\ \hline
\end{tabular}
\end{itemize}







\section{Movement by lines --- Examples}
\label{sec:movement-by-lines-examples}

\begin{itemize}
  \item In each case here, we start from: \\[12pt]
{\cmdn This, he said, \\
is a \framebox[10pt]{m}ost \\
interesting example} \\[12pt]

\begin{tabular}{|p{63pt}|p{270pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn + <RET>} & {\cmdn This, he said,}\\
   & {\cmdn is a most} \\ 
   & {\cmdn \framebox[10pt]{i}nteresting example} \\ \hline
{\cmdn -} & {\cmdn \framebox[10pt]{T}his, he said,} \\
   & {\cmdn is a most} \\ 
   & {\cmdn interesting example} \\ \hline
{\cmdn 0 or \verb|^|} & {\cmdn This, he said,} \\
   & {\cmdn \framebox[10pt]{i}s a most} \\ 
   & {\cmdn interesting example} \\ \hline
{\cmdn \$} & {\cmdn This, he said,} \\
   & {\cmdn is a mos\framebox[10pt]{t}} \\ 
   & {\cmdn interesting example} \\ \hline
\end{tabular}
\end{itemize}



\section{Inserting text}
\label{sec:inserting-text}

\begin{itemize}
  \item You probably want more from a text editor than the ability to move a cursor!
  \item At the bare minimum you need to be able to insert text into a file
  \item Don't worry, {\cmdn vi} does this with ease
  \item As with everything else, though, there's more than one way
  \item Again, while this may seem confusing, you only {\em need} to know the bare minimum
  \item {\em But}, the more you know, the easier your life becomes!
\end{itemize}



\section{{\cmdn i} command}
\label{sec:insert command}

\begin{itemize}
  \item The {\cmdn i} command inserts text before the cursor
  \item This places {\cmdn vi} into `insert' mode
  \item Anything you type now is treated as text to insert into the file rather than as a command
  \item You leave insert mode by typing {\cmdn <ESC>}
  \item This is insertion at its simplest!
  \item To insert text {\em after} the cursor we use the {\cmdn a} (append) command
  \item Also : \\[12pt]
{\myss \begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn A} & Append at the end of the line\\ \hline
{\cmdn I} & Insert at the beginning of the line \\ \hline
{\cmdn o} & Create blank line below cursor for insertion \\ \hline
{\cmdn O} & Create blank line above cursor for insertion \\ \hline
\end{tabular}} \\[12pt]
  \item If your cursor keys work then you may move around the line while in insert mode
  \item You can delete characters from the current insertion using backspace
\end{itemize}



\section{Multiple Insertion}
\label{sec:mutiple-insertion}

\begin{itemize}
  \item Insertion commands can take numeric prefixes
  \item The result may be surprising!
  \item Consider the following sequence of keypresses (from command mode) in an empty document
\begin{verbatim}
5i&nbsp;
\end{verbatim}
\item The result will be :
\begin{verbatim}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
\end{verbatim}
\end{itemize}




\section{Deleting Text}
\label{sec:deleting-text}

\begin{itemize}
  \item {\cmdn vi} has a vast array of commands for deleting text
  \item The `odd-one-out' is {\cmdn x} which deletes the character under cursor
  \item The rest of the deletion commands are based-around the easy to remember {\cmdn d} command
  \item {\cmdn d} on its own does nothing
  \item You have to tell it how much to delete
  \item The amount to delete is given by the keys you used when studying movement \\[12pt]

Example: \\[12pt]
{\myss \begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn dw} & Delete to the beginning of the next `word' \\ \hline
{\cmdn 3dw} & Delete 3 `words' \\ \hline
{\cmdn de} & Delete to the end of the `word' \\ \hline
{\cmdn db} & Delete everything before cursor to the beginning of the `word' \\ \hline
{\cmdn d\$} & Delete to the end of the line \\ \hline
{\cmdn d0} & Delete to the beginning of the line \\ \hline
\end{tabular}} \\[12pt]
  \item Two more special cases : \\[12pt]
{\myss \begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn dd} & Delete the entire line \\ \hline
{\cmdn D} & Delete to the end of the line \\ \hline
\end{tabular}}
\end{itemize}



\section{Changing Text}
\label{sec:changing-text}

\begin{itemize}
  \item Now we know everything we need to know to delete text, insert new text and save changes
  \item {\cmdn vi} however likes to give us choices!
  \item If we find a word that is wrong, we can delete it and insert the replacement
  \item We're {\em actually} `changing' the word
  \item {\cmdn vi} has a family of commands for just this, all starting with {\cmdn c}
  \item Similar to deletion, i.e. you can use {\cmdn cw} to change a word, {\cmdn c\$} to change to the end of the line, or {\cmdn 3cw} to change three words
  \item What actually happens is that the designated amount is deleted and you are placed in insert mode \\[12pt]
{\myss \begin{tabular}{|p{30pt}|p{303pt}|} \hline
Key & Result \\ \hline \hline 
{\cmdn cw} & Change a word \\ \hline
{\cmdn 3cw} & Change 3 words \\ \hline
{\cmdn c\$} & Change to the end of the line \\ \hline
{\cmdn c0} & Change to the beginning of the line \\ \hline
\end{tabular}}
\end{itemize}



\section{Copy and Paste}
\label{sec:copy-and-paste}

\begin{itemize}
  \item We're still missing the ability to copy a piece of text and paste it somewhere else
  \item {\cmdn vi} does support this, but it calls it `yanking' and putting
  \item All `yanking' commands are prefixed with a {\cmdn y} and follow the same rules as before, i.e. {\cmdn yw}, {\cmdn y\$}, {\cmdn 3yw}
  \item {\cmdn yy} and {\cmdn Y} yank a whole line and the rest of a line, respectively
  \item Paste text using {\cmdn p} or {\cmdn P}
  \begin{itemize}
    \item {\cmdn p} pastes text {\em after} the cursor
    \item Uppercase {\cmdn P} pastes it {\em before}
  \end{itemize}
  \item Deleted text is also considered to be yanked
  \begin{itemize}
    \item {\cmdn xp} will transpose two characters
  \end{itemize}
\end{itemize}



\section{Finding your place}
\label{sec:finding-your-place}

\begin{itemize}
  \item You can search through a file using {\cmdn /}
  \item You will get a {\cmdn /} as a prompt on your status line
  \item To search for the string {\cmdn exam} type
\begin{verbatim}
/exam
\end{verbatim} and press {\cmdn <RETURN>}
  \item If {\cmdn vi} found your search string it will move the screen to a relevant place and highlight it
  \item {\cmdn n} will skip to the next occurence; {\cmdn N} to the previous
  \item Search backwards by using {\cmdn ?} instead of {\cmdn /}
\end{itemize}




\section{Miscellaneous Commands}
\label{sec:misc}

\begin{itemize}
  \item {\cmdn vi} has a number of commands that don't really fit anywhere else
  \item {\cmdn \verb|~|} toggles the case of character under cursor
  \item {\cmdn .} repeats the last action
  \item {\cmdn u} undoes the last action
  \begin{itemize}
    \item Linux {\cmdn vi} supports multilevel undo
    \item Standard {\cmdn vi} does {\em not}
  \end{itemize}
  \item {\cmdn J} Join the current and following line
\end{itemize}



\section{Search and replace}
\label{sec:search and replace}

\begin{itemize}
  \item {\pgn vi} can also replace the words it finds
  \item Basic form is:
    \begin{quote}
      {\cmdn s/searchfor/replacewith/modifier}
    \end{quote}
  \item By default it only changes one occurence per line, and only checks the current line
  \begin{itemize}
    \item If we tag the {\cmdn g} modifier on the end it will replace all matches on the current line
  \end{itemize}
  \item If we use a range\footnote{Not explained here, this is an advanced topic} we can search and replace a specified part of a document, e.g.
  \begin{itemize}
    \item To search and replace from lines 10 to 15 inclusive:
    \begin{quote}
      {\cmdn :10,15 s/foo/bar/g}
    \end{quote}
    \item To search and replace on the whole document
    \begin{quote}
      {\cmdn :1,\$ s/foo/bar/g}
    \end{quote}
  \end{itemize}
\end{itemize}



\section{Regular Expressions}
\label{sec:regexps}

\begin{itemize}
  \item Sometimes it's desireable to search for a word `fuzzily'
  \item You may know the start of a word, or the end
  \begin{itemize}
    \item Or both, but not the bit in the middle!
  \end{itemize}
  \item {\em Regular expressions} can come in useful here
  \item Can be used in normal searches or `search and replace' commands
\end{itemize}


\section{Regular Expression Conventions}
\label{sec:regexp-conventions}

\begin{itemize}
  \item Lots of things in Linux use regular expressions
  \begin{itemize}
    \item Not all `exactly' the same
    \item 95\% similar though
  \end{itemize}
  \item Defines certain {\em special characters} \\[12pt]
{\myss \begin{tabular}{|p{64pt}|p{269pt}|} \hline
Character & Result \\ \hline \hline 
{\cmdn .} & Match any character \\ \hline
{\cmdn [a-z]} & Match any character in the range a to z \\ \hline
{\cmdn *} & Match the preceeding character zero or more times \\ \hline
{\cmdn \verb|^|} & Match the beginning of a line \\ \hline
{\cmdn \$} & Match the end of a line \\ \hline
{\cmdn \verb|\|<} & Match the beginning of a word \\ \hline
{\cmdn \verb|\|>} & Match the end of a word \\ \hline
\end{tabular}} \\[12pt]
  \item Strictly speaking * can apply to more than one character
  \begin{itemize}
    \item We won't cover that here
  \end{itemize}
\end{itemize}



\section{Regular Expression Examples}
\label{sec:regexp-examples}

\begin{itemize}
  \item Suppose we want to find all words ending in {\cmdn ent}
  \item We could read the entire document to check for {\cmdn ent} by hand
  \begin{itemize}
    \item Takes far too long
    \item We'd probably still miss some
    \item Easier to get the computer to do it
  \end{itemize}
  \item We could do a search using {\cmdn /ent<RET>}
  \begin{itemize}
    \item Unfortunately that would also match words beginning with {\cmdn ent} or with {\cmdn ent} in the middle
  \end{itemize}
  \item {\cmdn /ent\verb|\|>} will jump to the next word that ends with {\cmdn ent}
\end{itemize}


\section{Regular Expression Replacement}
\label{Sec:regexp-replace}

\begin{itemize}
  \item We can also use regular expressions in the search section of search and replace commands, e.g.
\begin{verbatim} s/\<foo/bar/g
\end{verbatim}
will replace all occurences of {\cmdn foo} at the beginning of a word with {\cmdn bar}
\end{itemize}

  
\section{Help}
\label{Sec:help}

\begin{itemize}
\item {\cmdn vi} on Linux has very extensive online help.

\item There is an interactive tutorial too; try it out.
% Nick: TODO: explain how the hyper links in help work
\end{itemize}




\section{{\pgn vi} Exercises}
\label{sec:vi-exercises}
{\normalsize



\begin{enumerate}
\item {\em Recognizing {\cmdn vi} }

  \begin{enumerate}
    \item Start up {\cmdn vi} with no filename to see what it looks like
    \item Exit {\cmdn vi} and then start it again with the file {\fn /etc/passwd}
    \item What can you tell about the file from this screen?
  \end{enumerate}




\item {\em Getting used to {\cmdn vi}}


  \begin{enumerate}
    \item Start {\cmdn vi} with the the file {\fn /etc/passwd} again
    \item Practise the basic movement commands on the file
    \item Check you can use both the cursors and {\cmdn hjkl} to move around
    \item Check the other movement commands work as expected
  \end{enumerate}

{\em   \item Creating with {\cmdn vi} }

  \begin{enumerate}
    \item Start {\cmdn vi} with the filename {\fn vi\_test}. This should be a new file
    \item Insert your name into the file and then save it and leave {\cmdn vi}
    \item Open the file again and check it still contains your name
    \item Next add some more names to the file, one on each line
    \item Go to a name roughly half way down your list. Check you can insert a name on the line above, and on the line below
    \item Check you can append to the end of lines and insert at the beginning of lines
  \end{enumerate}



  
   \item {\em Movement and Multipliers}


  \begin{enumerate}
    \item Check you can move through your file using combinations of the movement keys and numeric prefixes. \\ For example
    \begin{enumerate}
      \item Move 3 lines down at a time
      \item Move 2 words along
      \item Move to the beginning of the second line below your cursor
    \end{enumerate}
  \end{enumerate}
 {\em   \item Deleting with {\cmdn vi} } 
  \begin{enumerate}
    \item Try deleting various entities (Words, lines, characters) from your file
    \item Check that these work with the numeric prefixes
    \item You should be able to achieve all of the following
    \begin{enumerate}
      \item Delete a word
      \item Delete to the end of the line
      \item Delete to the beginning of the line
      \item Delete the whole line
      \item Delete 2 lines at once
      \item Delete 2 words at once (Either including or excluding punctuation)
    \end{enumerate}
  \end{enumerate}

 



\item {\em Changes with {\cmdn vi}}


  \begin{enumerate}
    \item Repeat the exercises given for delete but do changes instead of deletions
  \end{enumerate}



\item {\em  Yanking and Pasting } 


  \begin{enumerate}
    \item Copy the first line of your file and paste it so that it becomes the last line
    \item Paste it back at the top of the file
    \item Place the cursor at the very beginning of the file and try the following keystrokes
    \begin{enumerate}
      \item {\cmdn yyjyyp}
      \item {\cmdn 2yyp}
    \end{enumerate}
    \item What was the difference and can you suggest why this may be?
    \item Check that text deleted can be pasted back
  \end{enumerate}



\item {\em Miscellaneous }

  \begin{enumerate}
  \item Place the cursor at the beginning of the file and try the following command sequence: \\
    {\cmdn yyp...} \\
    Explain the result
  \item Place the cursor over a letter on the middle of a word.  What happens when you type {\cmdn xp}?
  \item Join all the lines of your file into one long line. Check that
    the movement commands regarding lines work on {\em actual} lines
    rather than the lines as seen on your screen
  \end{enumerate}

\end{enumerate}



















\section{{\pgn vi} Solutions}
\label{sec:vi-solutions}


\begin{enumerate}
 \item {\em Recognizing {\cmdn vi}}

  \begin{enumerate}
  \item Check you understand where the status line is, and what the
    \verb|~| characters mean
  \item {\cmdn :q} should exit {\cmdn vi}. If you want to make sure
    you're in command mode press {\cmdn <ESC>} first.  {\cmdn vi /etc/passwd}
    will start {\cmdn vi} with {\fn /etc/passwd} opened
  \item {\cmdn vi} should tell you that this file is read only. This
    is because you don't have sufficient permissions to change the
    file. {\cmdn vi} should also tell you how many lines and
    characters are in the file.
  \end{enumerate}
 {\em \item Getting used to {\cmdn vi} } 
  \begin{enumerate}
  \item {\cmdn vi /etc/passwd}
  \item You should be fairly comfortable with the various navigation
    methods such as moving left, right, up and down, to the end or
    beginning of the line and moving up and down by intervals of pages
    and half pages.
  \end{enumerate}





 \item {\em Creating with {\cmdn vi}}


  \begin{enumerate}
  \item {\cmdn vi vi\_test}.  The status line should tell you that it
    is a new file and each line on the main screen should begin with a
    \verb|~| indicating lack of content
  \item To insert my\_name simply type: \\
    {\cmdn imy\_name<ESC>} \\
    There are several ways to save and exit: \begin{enumerate}
    \item {\cmdn :w} followed by {\cmdn :q}
    \item {\cmdn :wq}
    \item {\cmdn ZZ}\end{enumerate}
  \item {\cmdn vi vi\_test} and check that the text you entered is
    there. If not try again.
    
  \item There are several ways to do this:
    \begin{enumerate}
    \item When inserting using {\cmdn i} you may type RETURN to insert a newline character. it is possible therefore to start with the cursor at the beginning of the file and type: \\
      {\cmdn iname1<RET>name2<RET>name3<RET>} and so on
    \item Typing {\cmdn o} or {\cmdn O} will open a new line for
      insertion
    \end{enumerate}
  \item You should check that you understand which of {\cmdn o} and
    {\cmdn O} inserts above, and which below the current line
  \item Appending to the end of a line can be done using either:
    \begin{enumerate}
    \item {\cmdn \$atext\_to\_append<ESC>}
    \item {\cmdn Atext\_to\_append<ESC>}
    \end{enumerate}
    Inserting at the beginning can be done using any of:
    \begin{enumerate}
    \item {\cmdn \verb|^|itext\_to\_insert<ESC>}
    \item {\cmdn 0itext\_to\_insert<ESC>}
    \item {\cmdn Itext\_to\_insert<ESC>}
    \end{enumerate}
  \end{enumerate}




\item {\em Movement and Multipliers}


  \begin{enumerate}
  \item You should practice moving around using the movement
    characters with the numerical prefixes
    \begin{enumerate}
    \item {\cmdn 3j}, {\cmdn 3+}, or {\cmdn 3<RET>}
    \item {\cmdn 3e}, or {\cmdn 2w}
    \item {\cmdn 2+}, or {\cmdn 2<RET>}
    \end{enumerate}
  \end{enumerate}





 \item {\em Deleting with {\cmdn vi}}


  \begin{enumerate}
  \item You should make sure that the various deleting methods work as
    you expected. If they surprise you, try to work out how they {\em
      do} work.
  \item Again check you understand the various possibilities.
  \item The following represent only possible solutions:
    \begin{enumerate}
    \item {\cmdn dw}
    \item {\cmdn d\$}
    \item {\cmdn d0}
    \item {\cmdn dd}
    \item {\cmdn 2dd}
    \item {\cmdn 2dw}
    \end{enumerate}
  \end{enumerate}




  
\item {\em Changing with {\cmdn vi}}

  \begin{enumerate}
  \item The answers for this are the same as for delete except
    substituting {\cmdn c} for {\cmdn d} in each case.
\end{enumerate}




  
\item {\em Yanking and Pasting} 


  \begin{enumerate}
  \item Move to the first line of the file and type {\cmdn yy}, then
    move to the end of the file and type {\cmdn p}
  \item Move back to the top line of the file and type {\cmdn P} which
    will paste it above the current line.
  \item Check you can tell the difference between the two commands.
  \item The `Yank buffer' only holds the contents of one yank
    operation. Both sets of keypresses yank the line we start on and
    the line below. However the first does this as two seperate
    operations and the `yank buffer' only remembers the most recent.
    The second example yanks two lines at once, therefore placing both
    in the yank buffer.
  \item You should try {\cmdn ddp} and check that the text appears
    after being deleted.
 \end{enumerate}



  
\item {\em Miscellaneous} 


  \begin{enumerate}
  \item {\cmdn .} repeats the last action. In this case it is a paste
    operation. It could equally well have been an insert, change word
    or delete operation.
  \item The {\cmdn xp} command pair is useful for transposing letters.
  \item Starting at the top of your file pressing {\cmdn J} will join
    the following line to the current line.  Repeat this until the
    entire file is on one line. Pressing one of the down a line keys
    (Such as {\cmdn j}, {\cmdn +} or {\cmdn <RET>} should have no
    effect despite the illusion that there is more than one line.
  \end{enumerate}


\end{enumerate}

} % end {\normalsize} from start of exercises
} % end {\mns} from chapter start

\label{endofchapter-intro2vi}




%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "planet_basic_0_masterfile"
%%% End: 
