31
Editors A Brief History Early programmers used punch cards Make a mistake? Make a new card Editors are associated with terminals First terminals were teletypes – “wired” typewriters Teletypes printed one line at a time

Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsA Brief History

Early programmers used punch cards

Make a mistake? Make a new card

Editors are associated with terminals

First terminals were teletypes – “wired” typewriters 

Teletypes printed one line at a time

Page 2: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

A Brief History – Line Editors

For teletypes, use a line editor

Line editors make corrections on a line­by­line basis

Give a line number or multiple line numbers and execute a command on that line or those lines

Page 3: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsA Brief History – Full Screen Editors

Later terminals were based on cathode ray tubes and allowed for screen drawing

Screen drawing meant manipulation of characters on the screen 

Access to the full screen led to the development of full screen editors

Page 4: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsA Brief History – Full Screen Editors

Full screen editors allow the user to move the cursor to any point in the file and modify text in place

Full screen editors allow the marking, copying, cutting and pasting of full sections of text. Line editors allowed this as well but not as easily

The mouse (pointing device) made this process even simpler

Page 5: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors

Many editors now available for use in UNIX:

● vi● emacs/xemacs ● joe● pico/nano● nedit● Open Office?

Page 6: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors

Learn the most crucial commands first:

● Starting the editor● Moving around the file● Inserting text● Deleting text● Undoing changes● Saving the text● Exiting the editor

Page 7: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors ­ pico/nano

pico comes with the Pine mail package. nano is the “free” version of pico

pico is easy to use. Commands are listed at the bottom of the screen for easy reference

Commands are tied to control characters

Page 8: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors ­ pico/nano

Exercise – Creating a New File

Open a new file# nano myfile

Enter some text

Save the file with CTRL­X, answer “yes” and hit ENTER

Page 9: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ pico/nano

Exercise – Copying and Pasting Text

Open an existing file# nano myfile

Position the cursor at a line of textEnter CTRL­K to cut that lineEnter CTRL­U to paste that line multiple times. Make about 30 copies of the lineSave the file (CTRL­X, Yes, ENTER)

Page 10: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ pico/nano

Exercise – Moving Around the File

Open an existing file# nano myfile

Use the arrow keys to move the cursor

User CTRL­V to move down a page

Use CTRL­Y to move up a page

Page 11: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ pico/nano

Exercise – Finding Text (Searching)

Open an existing file (if not already open)# nano myfile

User CTRL­W to find a particular word or phrase

Page 12: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ pico/nano

Exercise – Inserting Text from Another File

Open an existing file (if not already open)# nano myfile

User CTRL­R to read in the contents of your bash startup script (~/.bashrc)

Without exiting the editor, save the file to another file called myfile2 using CTRL­O

Page 13: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ pico/nano

Exercise – Getting Help

Open an existing file (if not already open)# nano myfile

User CTRL­G to see the help documentation on other commands

Page 14: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ vi

vi is the oldest but most used screen editor in UNIX. 

It inherits the commands from the UNIX line editors ed and ex

vi gets its name from being the visual editor

Page 15: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors ­ vi

vi has two modes:

● Command – you enter line editor commands and extended commands

● Insert – as its name suggests, you insert text in this mode

Page 16: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Full Screen Editors – viInsert Mode

vi begins in command mode. To enter insert mode, enter the i key

Text can then be freely entered and deleted and, in some incarnations of vi, you can use your cursor to move around the file

Exit insert mode and return to command mode using either the Escape key or CTRL­[

Page 17: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Command Mode ­ Movement

● One character at a time ­  j,k,l or h keys or the arrow keys

● One word at a time:

w – beginning of the next word

e– end of the next word

b – beginning of the previous word

Page 18: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Command Mode ­ Movement

● One line at a time ­  CTRL­N, CTRL­P

● One half page at a time – CTRL­D, CTRL­U

● One full page at a time – CTRL­F, CTRL­B

● File at a time – SHIFT­G, 1 SHIFT­G

Page 19: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Modifying and Saving a File

Open a file# vi myfile

Go into insert mode with the i key

Enter some text

Go to command mode with the Escape key

Exit and save the file with :wq!

Page 20: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Moving Around the File

Open a file# vi myfile

Perform single character, single word, single line and page movement

h,j,k,l and arrow keysb, w, eCTRL­N, CTRL­PCTRL­D,CTRL­U,CTRL­F,CTRL­B

Exit without saving the file with :q!

Page 21: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Searching for Text

Open a file

# vi myfile

Look for a word using the search command key / 

Exit without saving the file using :q!

Page 22: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Copying and Pasting

Open a file# vi myfile

Pick a line and move the cursor to that line

Enter CTRL­Y to copy that line

Move to another part of the file

Use the p key to paste the line

Page 23: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Cutting and Pasting

Open a file# vi myfile

Pick a line and move the cursor to that line

Enter dd to cut that line

Move to another part of the file

Use the p key to paste the line

Page 24: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Applying Commands to Multiple Lines

Remember – vi is based on a line editor. To apply a command many times, enter the number of lines before entering the command

Open a file# vi myfile

Pick a line and move the cursor to that lineEnter 3dd to cut three linesMove to another part of the fileUse the p key to paste the lines

Page 25: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Line Editor Commands

vi inherits all the commands used by ed and ex. In command mode, precede one of these commands with a colon (:). Some useful commands include:

● :i ­ insert before ● :a ­ append after● :s ­ substitute text on lines that match a pattern

For both :i and :a, exit the insert mode by typing a period (.) on a line by itself

Page 26: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsFull Screen Editors – vi

Exercise – Substituting Text

The form of the substitution command is

:s/pattern/replacement/

Move the cursor to a line and try to substitute one word for another

:s/this/that/

Page 27: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsPattern Matching – Regular Expressions

Pattern matching uses a sophisticated and powerful symbolic language known as regular expressions

A complicated pattern can be built from a few (of many) simple symbols:

● ^ Matches the beginning of the line● $ Matches the end of the line● . Matches any single character● * Will match zero or more occurrences of the previous character● [ ] Matches all the characters inside the [ ]

Page 28: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsPattern Matching – Regular Expressions

Examples:

th.* – would match both this and that

now .*s the time – would match now is the time and now was the time

^#  ­ would match all comments in a shell script

Page 29: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Pattern Matching – Regular Expressions

Regular expressions are used by many UNIX commands and programming languages for pattern matching:

● Sed – the stream editor● Grep – get regular expressions● Awk● Perl

Page 30: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

Editors

Pattern Matching – Regular Expressions

sed

sed can be very powerful in shell scripts by modifying text on the fly using pipes

Examples:

● /usr/bin/tty | sed ­e 's/\/dev\/pts\//My tty number is /'● /sbin/ifconfig | sed ­n ­e 's/.*inet addr:\(.*\)Bcast.*/\1/p'

Page 31: Editors - Iowa State University · Full Screen Editors pico/nano Exercise – Inserting Text from Another File Open an existing file (if not already open) # nano myfile User CTRLR

EditorsLine Editor Commands

What to do if your system fails and you only have access to the root partition?You may only have ed.

# ed myfile1,$p1ps/now/then/wq