Upload
spencer-rice
View
220
Download
0
Embed Size (px)
Citation preview
Introduction to Unix – CS 21
Lecture 8
Lecture Overview More detail on emacs and vi Regular expression matching in
emacs and vi
Accessing All Emacs Commands Meta-x Tab completion
Exploring all of the different commands
Approximately 1800 of them
Quick Jump To A Line In vi G
By itself, it jumps to the end of the file 3G
Jumps to line 3 Any number works
Quick Jump To A Line In emacs Meta-x goto-line
Then type in the line to go to Which is faster? Which is better? Which is more conformant to Unix
style?
Search And Replace In Vi :s/old/new
Replaces old with new on the current line :s/old/new/g
Replaces all old with new on the current line
:1,$ s/old/new/g Replaces all old with new in the whole file
Query-Replace In Emacs Meta-% Meta-x query-replace Prompts you for old pattern and
new pattern Questions you on every instance if
you would like to replace it
Change In vi C will delete the line you’re at and
put you into insert mode “Changing” the current line
c works much like d cc = change line cw = change word c10 = change next ten lines
Replace In vi From command mode:
r will replace a single character R will put you into “Replace” mode
Everything you type overwrites what was previously there
Overwrite Mode In Emacs Hitting the Insert key changes
modes in emacs Takes you into and out of overwrite
You suddenly will find yourself overwriting instead of inserting Check for (Ovwrt)
Emacs Buffer Primer When emacs is run without any
parameters, it opens up a buffer called *scratch*
All files opened and all messages that pop up are stored in different buffers and are always accessible
Window vs. buffer Just because you can’t see it, doesn’t mean
it is gone
Switching Buffers And Multiple Windows Cntrl-x, cntrl-b
List all buffers Cntrl-x b
Switch to buffer Cntrl-x 4 b
Switch to buffer in another window
Adding Or Getting Rid Of Windows Cntrl-x 2
Add another window (vertically) Cntrl-x 3
Add another window (horizontally) Cntrl-x 1
Only show one window
Read Only Files In Emacs %% will appear on the bottom of
the screen indicating that the file is read-only
You won’t be able to change the file in any way, you’ll just be able to read it
Read Only Files In vi vi will warn you when you attempt to
modify a read-only file It will let you change the file, though
If you attempt to save the changes, it will warn you again
Using the !, you can force the changes vi will let you modify a read-only file!
The Mark And The Point In order to highlight large sections
of text, emacs introduces the idea of the mark and the point
Mark Set with Cntrl-<space> The position of the cursor when set
Point Wherever the cursor is located
Using The Mark And Point Cntrl-w
Kills (cuts) the region from the mark to the point
Forwards or backwards doesn’t matter
Some actions automatically set the mark Cntrl-y
Pasting (yanking) sets the mark
Parenthesis Matching In Emacs Emacs will warn you when parenthesis
are mismatched () [] {}
Emacs will always tell you what a parenthesis matches On screen
Temporarily highlight Off screen
State what matches
Accessing Unix Commands in vi vi has access to the shell and can
run commands :!COMMAND
Will allow you to run one command !!COMMAND
Replace the current line with output of the command
Inserting Files And Commands In vi :r FILE
Insert the contents of FILE directly where the cursor is
!} COMMAND Pipe the contents of the current
paragraph into COMMAND and replace the current paragraph with the output
Accessing Unix Commands In Emacs Oh yeah? Watch this! Meta-x shell
Actually opens up a shell (command line) INSIDE of emacs that allows you to run any program and still move around, cut and paste, and do anything you want
The shell is located in another buffer *shell*
Regular Expression Matching In vi Just like searching for a normal
pattern Syntax is mostly the same as grep Some symbols must be escaped
\? \+ \( \)
Character sets are escape characters
Character Sets \d = digit \D = non-digit \a = alphabetic character \A = non-alphabetic character \l = lowercase character \L = non-lowercase character \u = uppercase character \U = non-uppercase character
Special Syntax Magic
. * $ treated as special characters This is the default mode
No Magic * . $ treated as normal characters Must be escaped
Regular Expression Matching In Emacs Cntrl-Meta-S
Regular expression search forwards Cntrl-Meta-R
Regular expression search backwards Syntax is mostly the same as grep
Some characters must be escaped \( \)
Colors In Vi Special types of files are
recognized Programming languages
Objects are colored depending on their status Keywords, comments, variables are
all colored differently
Colors In Emacs Emacs as well has colors, but not
necessarily on by default Meta-x font-lock-mode
Programming Modes In Emacs More than just colors
C and C++ files HTML files VHDL files Perl, prolog, ml, lisp, shell programming … Pretty much any language you can think
of has had a mode in emacs written for it Some are more extensive than others
Startup Files .emacs
Contains all the commands that get executed every time you start emacs
Sets default values .XXXrc
General form of startup files
Fun With Emacs Doctor Adventure Solitaire Pong Tetris!
In Lab Today You will play around with both
Emacs and vi You will try out both basic file
editing as well as some of the more advanced features
Next Week We look more at shells and
specifically we look at special features of bash
Midterm next Thursday Covers all material up from the first
day of class until next Tuesday