introduction to unix – cs 21 lecture 7. lecture overview regular expressions revisited emacs...

37
Introduction to Unix – CS 21 Lecture 7

Upload: dayna-fleming

Post on 14-Jan-2016

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Introduction to Unix – CS 21

Lecture 7

Page 2: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Page 3: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Back To Regular Expressions You will see regular expressions

pop up over and over again Important to understand how they

work Always important to keep in mind that

syntax can change, but the meaning will be the same

grep syntax is just as good as any

Page 4: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Guidelines On Constructing Regular Expressions Break it up piece by piece

If asked for a regular expression that finds two things, try to come up with regular expressions that find each individually

Combine multiple R.E.’s with “or” (|) If I want all lines with “hello” in them,

and you have two expressions that find different instances, one expression with an or will get all of them

Page 5: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Example Find all instances of dog but not

dogs “dog[^s]”

finds at the beginning or middle “dog$”

finds at the end “(dog[^s])|(dog$)”

finds all instances

Page 6: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

More Guidelines Think about all cases

Beginning, middle, and end of the line Think about what else is important

on the line If I ask for lines that start with “a” and

end with “e”, also think about what could go in the middle

Page 7: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

In-Class Exercise Come up with regular expressions

for the following: All lines that have a phone number in it

909-992-1101 or 123-4567 All lines that contain the word “lean”

but not the word “cleans” All lines that start with “And” and don’t

end with “of”

Page 8: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

In-Class Exercise Discussion “[0-9][0-9][0-9]-[0-9][0-9][0-9][0-

9]” “[0-9]{3}-[0-9]{4}”

([^c]lean[^s])|(^lean)|(lean$) (^And.*[^f]$)|(^And.*[^o]f$)

Page 9: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Actually Creating And Modifying Files Good News

Multiple editors in Unix exist that allow you to create and modify text files

Bad News You will have to get used to and

memorize a whole bunch of commands in order to use them well

Page 10: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Editors In Unix vi

The “visual” editor vim = “vi improved”

emacs “editor macros” “A way of life”, not just an editor

Page 11: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

“Holy War” The choice between emacs and vi

divides a large part of the Unix community

No real answer as to which is better, just which you prefer to use Most likely what you started out with

People, will argue endlessly over why one is better than the other

Page 12: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Why Would You Use vi? Quick and small

Ready to use immediately and it doesn’t consume too many resources

Simple No hassles, but quite a few features that are

helpful Always there

vi is virtually guaranteed to be on every Unix system, emacs isn’t

Page 13: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Why Would You Use emacs? Large Specialized

Modes exist that help you out for many different types of files

Powerful Allows you to integrate a lot of work

from the shell without leaving the editor

Fast (relatively)

Page 14: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Which One Should You Choose? Whichever one you feel

comfortable with Both will be introduced here and you

will see the differences Try them both and figure out which

suits you best

Page 15: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Unix Editors Versus Windows Editors vi knows nothing about the mouse

vi doesn’t have any menu options either

If emacs is run inside a terminal window, then the mouse is useless as well

All conveniences can be done through keystrokes, not through mouse operations

Page 16: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Modes Of vi vi is a modal program

The same key means different things depending on what mode you are in

vi always begins in “command mode” You won’t be able to type in any text

until you change modes “Esc” will get you into command mode

Page 17: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

vi Basics Starting vi

Usage: vi [FILE] Quitting vi

Esc, :q

Page 18: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

emacs Essentials Always able to enter text Commands are given by

combinations of control keys Cntrl Meta

Usually “Alt” Sometimes “Esc” (Solaris, etc.)

Page 19: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

emacs Basics Starting emacs

Usage: emacs [FILE] Quitting emacs

Cntrl-x, Cntrl-c Book specifices Cntl-x, Cntl-h, Ctnrl-h,

Cntrl-h but that doesn’t work on our system

Page 20: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Opening A File In vi In command mode, hit :e

FILENAME This will abandon you previous file

Page 21: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Opening A File In emacs Cntrl-x, Cntrl-f This will switch you to a new file,

but will not abandon your previous file

Page 22: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Moving Around In vi With vim, the arrow keys, page up,

and page down work just fine On some systems, you will need to

use special keys in command mode Cntrl-f = page down Cntrl-b = page up h, j, k, l = work like the arrow keys

Left, down, up, right respectively

Page 23: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Specific Movement Commands In vi w = move forward one word b = move backward one word 0 = move to the beginning of a line $ = move to the end of the line Cntrl-u = move up half a page Cntrl-b = move down half a page

Page 24: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Moving Around In Emacs Arrow keys and page up, page

down work fine Special keys exist just in case

Cntrl-f = one character forward Cntrl-b = one character backward Cntrl-v = Page Down Meta-v = Page up

Page 25: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Special Movement Commands In emacs Meta-f = move forward one word Meta-b = move backward one word Cntrl-a = move to the beginning of the

line Cntrl-e = move to the end of the line Meta-a = move to the beginning of a

sentence Meta-e = move to the end of a sentence

Page 26: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Simple Searching In vi In command mode, type “/”

followed by the word you are looking for

To go to the next occurrence, in command mode type “n” Search forwards

“N” will go to previous occurrences in the file Search backwards

Page 27: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Simple Searching In emacs Cntrl-s

Type in the word you are searching for Searches the file forwards Cntrl-S again will move you to the

next occurance Cntrl-r

Works exactly the same way, but searches backwards through the file

Page 28: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Inserting In vi First, make get into “insertion

mode” i = transfer into insertion mode o = transfer into insertion mode in a

new line below the current line O = transfer into insertion mode in a

new line above the current line Now type as normal

Page 29: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Inserting In emacs Just type as normal I see this as the biggest advantage

emacs has over vi No special command is needed to

start inserting text Combined with the menus, emacs is a

little easier for beginners to handle

Page 30: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Deleting In vi x = delete one character dw = delete one word dd = delete one line d10 = delete 10 more lines (11

actually)

Page 31: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Deleting In emacs Cntrl-d = delete one character Meta-d = delete next word Cntrl-k = delete one line

Page 32: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Cut And Pasting In vi Whatever was just deleted is

available to be pasted Word, character, or block of lines

In command mode, “P” will paste most recently cut object

Page 33: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Cut And Pasting In emacs As long as you are deleting lines

with Cntrl-k, those lines can be pasted somewhere else

Cntrl-y will paste most recently cut lines

Page 34: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Saving Changes In vi In command mode, hit “:w” If you want to quit without saving

changes, you need to the command “:q!”

Page 35: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Saving Changes In emacs Cntrl-x, Cntrl-s If you want to exit without saving

changes, Cntrl-x, Cntrl-c as normal and then you will be prompted on if you really want to do that

Page 36: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Emacs Backup Files Emacs will automatically backup

the last version of a file whenever you save new changes Example~

Whenever a problem occurs, another file gets created #Example#

Page 37: Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage

Next Time We go into a lot more depth with

emacs and vi A lot of tricks that come in handy

will be presented