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

Post on 14-Jan-2016

234 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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

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”

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$)

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

Editors In Unix vi

The “visual” editor vim = “vi improved”

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

“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

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

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)

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

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

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

vi Basics Starting vi

Usage: vi [FILE] Quitting vi

Esc, :q

emacs Essentials Always able to enter text Commands are given by

combinations of control keys Cntrl Meta

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

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

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

FILENAME This will abandon you previous file

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

but will not abandon your previous file

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

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

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

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

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

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

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

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

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

actually)

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

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

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

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

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

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

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#

Next Time We go into a lot more depth with

emacs and vi A lot of tricks that come in handy

will be presented

top related