a line editor

ex filename - open a file with ex

ex commands consist of a line address (which can simply be a line number) plus a command; they are finished with a carriage retrun. one of the most basic commands is p for print.

:1p - print the first line of the file

infact you can leave off the p, cause a line number is equvialent to a print command. To print more than one line you can specify a range of line numbers

a command without a line number is assumed to affect the current line.

to invoke an ex comman from vi => :(colon) plus command plus Enter

:vi - invoke vi editor on file

Q in the command mode of vi invokes ex. Any time you are in ex, the command vi invokes vi editor on current file

Editing with ex

ex command:

fullName    Abbreviation

delete        d

move        m

copy        co

       t (a synonym of co)

three ways to specify line address
  1. with explicit line numbers
  2. with symbols that help you specify line numbers relative to your current postion in the file
  3. with search patterns as addresses that identify the lines to be affected
defining a range of lines

You can use line number to exxlicity define a line or range of lines.Address that useexplicity numbers are called absolute line address. For example

:3,18d - delete lins 3 through 18

:160,224m23 - move linse 160 through 244 to follow line 23.(Like delete and put in vi)

:23,29co100 - copy lines 23 through 29 and put after line 100 (Like yank and put in vi)

To make editing with line numbers easier , you can also display all line numbers on the left of screen:

:set number or its abbreviation: :set nu

disable line number:

:set nonumber or :set nonu

To temporarily display the line number for a set of lines, you can use # sign. For example:

:1,10#

Yet another way to identify line number is with ex = command:

:= - print the total number of lines

:.= - print the line number of current line

:/pattern/= - print the line number of the first line that matches pattern

Line Addressing Symbols

A dot(.) stands for the current line. Dollar($) stands for the last line of the file. Percent(%) stands for every line iin the file. These symbols can also combined with absolute line address.

:.,$d - delete from the current line to the end of file

:20,.m$ - move from line 20 throught current line to the end of file

:%d - delete all the lines in a fline

:%t$ - copy all lines and place them at the end of the file (making a consecutive duplicate)

You can specify an address relative to the current line. The symbol + and - are equivalent to +1 and -1. ++ and __ each extend the range by an additional line, and so on.

The number 0 stands for the top of the file. 0 is equivalent to 1-, and both allow you to move or cooy lines to the very start of the file, before the first line of existing text. For example:

:-,+t0 - copy three lines and put them at the top of the file

Search Patterns

another way that ex can address lines is by using search pattern.

:/patter/d - delete the next line caontaining pattern

:/pattern/+d - delete the next line containing pattern

:/pattern1/,/pattern2/d - delete form 1 to 2

:.,/pattern/m23 - take the text from the current line through the first line containing pattern and put it after 23

:d/while - delete from the cursor to the pattern word(no include)

When you use a semicolon instead of comma, the first line address is recaculated as the current line

:100;+5 p

g let you search for a pattern and display all lines containing the pattern when it finds them. The command :g! dose the opposite of :g. Use :g! (or its synonym, :v) to search for all lines that do not contain pattern

:g/pattern - find(move to ) the last occurrence of pattern in the file

:g/pattern/p - find and display all lines in the file containing pattern

:g!/pattern/nu - find and display all lines inthe file that don’t contain pattern, also displays the line number for each line found

:60,124g/pattern/p - find and display any lines between lines 60 and 124 containning pattern

Saving and existing files

:x - both write the file and quits the editor.

Renaming the buffer

You can also use :w to save the entire buffer to a new file name, save both the old version and new editing version

:w practice.new

after you can :q

Saving part of a file

:w newfile

Appending to a saved file

you can use the Unix redirect and append operator(>>) with w to append all or part of the contents of the buffer to an existing file.

:1,10w newfile
:340,$w >>newfile

Copying a file into another file

you can read in the contents of another file with ex command:

:read filename

or its abbrevations

:r filename

this command inserts the contents after the cursor. Type line number you want befoe read or r command to specify a line to insert

Invoking vi on Multiple Files

When you first invoke vi , you can name more than one file to edit and travel between them with ex commands

:n - travel to next file

vi text1 text2 => :w => :n => :x

Using the argument list

:args - display all opened files

:rewind (:rew) - reset hte current file to be the first file named on the command line. elvis and Vim provide a corresponding :last command to move to the last file on the command line

Calling in new files

if yo uwant to edit another file within vi, you first need to save your current file (:w), then give the command:

:e filename

vi ‘remembers’ two filenames at a times as the current and alternate filenames. These can be refered to by the symbols %(current filename) and #(alternate filename). # is particularly useful with :e, since it allow you to switch easily back and forth between two files.

:e # - switch back

:r # - read alternate file

if you have not saved the curernt file, vi will not allow you to swith files with :e or :n unless !

% is useful mainly when writing out the contents of current buffer to a new file:

:w %.new - % stand for the current file

CTRL + ^ is equvalent to :e #